Structures
1
Objectives
• Introduction to Structures
• Defining Structures
• Processing a structure
• User-defined Data types
• Passing structures to Functions
• Self-referential Structures
2
Introduction
Array is a data type to store multiple
values in the same data type.
Structure in another way is like array
but can store value from different
data type
An individual structure elements are
referred as member
3
Introduction
Array is a data type to store multiple
values in the same data type.
Structure in another way is like array
but can store value from different
data type
An individual structure elements are
referred as member
ID NO Name Address GPA
1123 John Li 121, Jalan Blue 3.78
4
How Structure look like?
Syntax
structure
tag
struct StructureTypeName
{
StructureMemberDeclarationsList
}; /*end struct*/
Example List of
declarations of its
struct faculty_struct
members
{
int faculty_idno;
char faculty_name;
int age;
char gender;
float GPA;
}; /*end faculty_struct*/
5
Declaring Structure Variables
After declaring a structure type, we may declare variables that
are of that type. A structure variable declaration requires these
elements:
[Link] keyword struct
[Link] structure type name
3.A list of variable names separated by commas
4.A concluding semicolon
struct tag {
datatype member1;
datatype member2;
datatype member3;
IMPORTANT:
}; Remember to put the “;”
at the end of structure
declaration
6
Structures
Itis possible to combine the declarations of a
structure type and a structure variable by
including the name of the variable at the end of
the structure type declaration.
struct faculty_struct {
int faculty_idno;
char faculty_name;
int age;
char gender;
float salary;
} faculty_record; /*end faculty_struct*/
Faculty_record in this case
is also called structure
variables
7
Example
struct cdCollection
{
char title[25];
char artist[20];
int numSongs;
float price;
char dataBought[9];
} cd1, cd2, cd3;
What is the structure tag?
How many members are there?
What are the member data types?
What are the member names?
How many structure variables are there?
What are their name?
8
Nested Structures
Since member of structure can be of any datatype
Therefore structure can also contain structure as
its member
struct tag1
{
int member1;
struct tag2 variable;
};
Where structure tag2 had to be declared before
tag1.
9
Processing a structure
Members of a structure are usually processed
separately
Therefore need to access the structure
member individually
Each member of the structure can be
accessed by
[Link]
To access a member of a nested structure;
[Link]
Where member is a structure type.
10
Example
based on the structure type faculty_struct [slide 5], and
the structure variable declaration
struct faculty_struct faculty_record;
faculty_record.faculty_idno = 1200;
strcpy(faculty_record.faculty_name, “Farid”);
faculty_record.age = 30;
faculty_record.gender = ‘M’;
faculty_record.salary = 7800.00;
struct cdCollection cd1;
strcpy([Link], “ Red Moon Men”);
strcpy([Link], “Sam and the sneeds”);
[Link]=12;
[Link]=11.95
Strcpy([Link], “02/02/07”);
11
Example printf("Here is the CD information: \n\n");
#include <stdio.h>
printf("Title: %s \n", [Link]);
#include <stdlib.h>
printf("Artist: %s \n", [Link]);
#include <string.h>
printf("Songs: %d \n", [Link]);
printf("Price: %.2f \n",[Link]);
int main()
printf("Date Bought: %s \n",
{ [Link]);
struct cdCollection
{ printf("\n");
char title[25]; system("PAUSE");
char artist[20]; return 0;
int numSongs; }
float price;
char dateBought[9];
} cd1;
strcpy([Link], "Red Moon Men");
strcpy([Link],"Sam and the
sneeds");
[Link] = 12;
[Link] = 11.95;
strcpy([Link], "02/02/07");
12
Initializing Structure Variables
InC it is possible to initialize structure variables at
compile time. Compile time initialization of a
structure variable requires the following elements:
[Link] keyword struct
[Link] structure type name
[Link] name of the structure variable
[Link] assignment operator, =
5.A set of values for the members of the structure
variable, enclosed in braces, and separated by commas
6.A terminating semicolon
13
Example
struct faculty_struct
{
int faculty_idno;
char faculty_name;
int age;
char gender;
float salary;
} faculty_record = {1200, “Farid”, 30, ‘M’, 7800.00};
struct cdCollection
{
char title[25];
char artist[20];
int numSongs;
float price;
char dataBought[9];
} cd1={“Red Men”, “Sam and the sneeds”, 12, 11.95, “02/02/07”};
14
Initialization Rules
1. Theorder of values for structure variable
members enclosed in braces must match the
order of members in the structure type
declaration.
2. It
is possible to initialize only a subset of the
members of a structure variable at compile
time. However, that subset must always begin
with the first member and continue until your
target member without skipping any.
15
User-defined Data Types
(typedef)
User can defined their own data type by
typedef type new-type;
Example:
typedef int age;
Typedef also can be use in structures
where
typedef struct {…} new-type;
16
Structure and Pointers
Ifvariable represents a structure-type variable, then
&variable represent the starting address of that
variable
Therefore a pointer can be declare for that particular
structure,
where
Struct {…} *ptvar;
And the member of the structure can be accessed by
ptvar->member;
17
Example
customer.acct_no = &n;
#include <stdio.h> customer.acct_type = &t;
#include <stdlib.h> [Link] = "smith";
#include <string.h> [Link] = &b;
int main() printf("\n%d\t%c\t%s\t%.2f\n",
{ *customer.acct_no, *customer.acct_type,
int n = 3333; [Link], *[Link]);
char t = 'c'; printf("\n%d\t%c\t%s\t%.2f\n", *pc->acct_no, *pc-
double b = 99.99; >acct_type, pc->name, *pc->balance);
struct printf("\n");
{ system("PAUSE");
int *acct_no; return 0;
char *acct_type; }
char *name;
double *balance;
}customer, *pc = &customer;
18
Operations on Structure Variables
The following operations are permitted on
structure variables:
◦ Copying the content of a structure variable to
another structure variable, provided both are of
the same structure type.
◦ Passing structure variables or their members to
functions.
19
Copying content between
structures
When you have two or more same
structure type, where
struct tag1{…} variable1, variable2;
Then
variable1 = variable2;
is a valid operation
20
Passing structure to Functions
There are several ways to pass structure-
type information to or from a function
Data can be pass o function by individual
member or the entire structures
Individual structure members treated as
ordinary single valued variable and can be
passed to a function as arguments in the
function call
21
Example of passing individual member
#include <stdio.h>
[Link] =
#include <stdlib.h> adjust([Link], customer.acct_no,
#include <string.h> [Link]);
struct record{ printf("\nRecord after adjustment using
char *name; individual pass");
int acct_no; printf("\n%s\t %d\t%c\t%.2f\n",
char acct_type; [Link], customer.acct_no,
float balance; customer.acct_type, [Link]);
};
printf("\n");
int main() system("PAUSE");
{ return 0;
float adjust(char *name, int acct_no, }
float balance); float adjust(char *name, int acct_no, float
balance)
static record customer {
={"Smith",333,'C',33.33}; float newbalance = 30.02;
return (newbalance);
printf("\nRecord before adjustment }
using individual pass");
printf("\n%s\t %d\t%c\t%.2f\n",
[Link], customer.acct_no,
customer.acct_type,
[Link]); 22
Example of Passing the whole Structure
#include <stdio.h> printf("\nRecord after adjustment passing to
#include <stdlib.h> function");
#include <string.h> printf("\n%s\t %d\t%c\t%.2f\n",
[Link], customer.acct_no,
typedef struct { customer.acct_type, [Link]);
char *name;
int acct_no; printf("\n");
char acct_type; system("PAUSE");
double balance; return 0;
}record; }
int main() void adjust(record *pt){
{ pt->name = "Jones";
void adjust(record *pt); pt->acct_no = 9999;
pt->acct_type = 'R';
static record customer pt->balance = 99.99;
={"Smith",333,'C',33.33}; return;
}
printf("\nRecord before adjustment to
function");
printf("\n%s\t %d\t%c\t%.2f\n",
[Link], customer.acct_no,
customer.acct_type, [Link]);
adjust(&customer);
23
Multiple block data in structures
One structure only can store one particular
block of data as in one array.
How about multiple block of data?
In array, we can have multidimensional array.
In structure, we can use structure array or
structure of data that linked to each other
through a pointer.
Structure of data that linked to each other
through a pointer also known as self-
referential structures or linked list
24
Structure array
int acctn;
#include <stdio.h> record *pt;
#include <stdlib.h>
#include <string.h> printf("\nAccount Record");
printf("\n--------------");
typedef struct{
char *name; for (acctn = 0; acctn <3; ++acctn)
int acct_no; {
char acct_type; pt = &customer[acctn];
double balance;
} record; printf("\n%s\t%d\t%c\t%.2f", pt->name, pt-
>acct_no, pt->acct_type, pt->balance);
}
int main()
{
printf("\n");
system("PAUSE");
static record customer[3] ={
return 0;
{"Smith",333,'C',33.33},
}
{"Jones",666,'C',66.66},
{"Brown",999,'C',99.99}
};
25
Self-referential structures
A structures that has a links (pointer) to
the next data that have same structure
Or a structure that have a member that
has the same structure as it owns.
Example
struct tag
{
datatype member1;
struct tag *ptvar;
}
struct list_element
{
char *item;
struct list_element *next;
} 26
Self-referentialstructures allow data to
linked together and provide flexible
operation and organization on the data.
The details of self-referential will be in
Data Structure and Algorithms (TMC1433).
27
Union
Defined as similar way as structure. The only
difference is that the union creates only one
memory space. The size of the memory
computed by the maximum size of its data
members.
Union student{
int id;
char name[10];
float gpa;
}u1;
Creates memory space of 10 bytes only
but similar structure creates memory space of 16 bytes
28
Example
[Link]=100; 100
[Link]=3.75;
Printf(“%d %f”,[Link],[Link]); Memory (10 bytes)
Output: <garbage value> 3.75
Union variable saves the latest assignment of its members
29
Example
[Link]=100; 3.75
[Link]=3.75;
printf(“%d %f”,[Link],[Link]); Memory (10 bytes)
Output: <garbage value> 3.75
Union variable saves the latest assignment of its members
30
Example
[Link]=100; 3.75
[Link]=3.75;
printf(“%d %f”,[Link],[Link]); Memory (10 bytes)
Output: <garbage value> 3.75
Union variable saves the latest assignment of its members
31
END
32