Structure
Structure
What is structure?
• Structureis a user-defined data type in C which allows you to
combine different data types to store a particular type of record.
• Structurehelps to construct a complex data type in more
meaningful way. It is somewhat similar to an Array.
• The only difference is that array is used to store collection of
similar datatypes while structure can store collection of any type of
data.
• Structure is used to represent a record.
• Suppose you want to store record of Student which consists of
student name, address, roll number and age. You can define a
structure to hold this information.
2
Structure Definition
• Keyword struct is used for creating a structure.
• Syntax of structure : struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeber;
};
• We can create the structure for a person as mentioned :
struct person
{
char name[50];
int no;
float salary;
};
3
Structure Variable Declaration
• When a structure is defined, it creates a user-defined type but, no
storage or memory is allocated.
• For the structure of a person, variable can be declared as:
struct person
{
char name[50];
int citNo;
float salary;
};
int main()
{
struct person person1, person2;
return 0;
}
4
Structure Variable Declaration
• Another way of creating a structure variable is:
struct person
{
char name[50];
int citNo;
float salary;
} person1, person2;
5
Accessing members of a structure
• Members of structures i.e. variables declared in a structure can be
assessed using period or member access operator(.)
• Any member of a structure can be accessed as:
structure_variable_name.member_name
6
Using typedef while using structure
• Writing struct structure_name variable_name; to declare a structure
variable isn't intuitive as to what it signifies, and takes some considerable
amount of development time.
• So, use typedef to name the structure as a whole. For example:
typedef struct complex
{
int imag;
float real;
} comp;
int main()
{
comp comp1, comp2;
}
• Here, typedef keyword is used in creating a type comp (which is of type
as struct complex).
• Then, two structure variables comp1 and comp2 are created by this
comp type. 7
Structure initialization
• Like any other data type, structure variable can also be initialized at
compile time.
struct Person
{
float height;
int weight;
int age;
};
struct Person p1 = { 180.75 , 73, 23 }; //initialization
or
struct Person p1;
p1.height = 180.75; //initialization of each member separately
p1.weight = 73;
p1.age = 23;
struct student
{
int mark1;
int mark2;
int mark3;
} sub1={67};
Though, there are three members of structure, only one is initialized ,
Then remaining two members are initialized with Zero (default value).
int will take 0, float/double will take 0.000000 and char will take NULL.
9
Size of Structure
• Different ways of calculating size of structure
• By observation
• By using sizeof() Operator
10
Size of Structure
• WAY 2 : Calculate by using sizeof() operator
11
Array of structures
• Structure is used to store the information of One particular object but if
we need to store such 100 objects then Array of Structure is used.
struct Bookinfo
{
char bname[20];
int pages;
int price;
}Book[100];
• Here Book structure is used to Store the information of one Book.
• Incase, if we need to store the Information of 100 books then Array of
Structure is used.
• b1[0]stores the Information of 1st Book , b1[1] stores the information
of 2nd Book and so on. We can store the information of 100 books.
12
Nested Structures
• Structure written inside another structure is called as nesting of two
structures.
• We can write one Structure inside another structure as member of another
structure.
struct date
• WAY 1 : Declare two separate structures {
int date;
• Structure members are accessed using dot operator. int month;
• ‘date‘ structure is nested within Employee Structure. int year;
• Members of the ‘date‘ can be accessed using ’employee’ };
• emp1 & doj are two structure names (Variables) struct Employee
{
char ename[20];
• Accessing Month Field : emp1.doj.month
int ssn;
• Accessing day Field : emp1.doj.day
float salary;
• Accessing year Field : emp1.doj.year struct date doj;
} emp1;
13
Nested Structures
• WAY 2 : Declare embedded structures
14
Passing structure to function
• There are mainly two ways to pass structures to a function:
• Passing by value
• Passing by reference
15
Passing structure by value
•A structure variable can be passed to the function as an argument as
a normal variable.
• Ifstructure is passed by value, changes made to the structure
variable inside the function definition does not reflect in the
originally passed structure variable.
• Function prototype should be below to the structure declaration
otherwise compiler shows error.
16
Passing structure by value
17
Passing structure by reference
• The memory address of a structure variable is passed to function
while passing it by reference.
• Ifstructure is passed by reference, changes made to the structure
variable inside function definition reflects in the originally passed
structure variable.
• Use the same concept of passing an address and receiving it in
pointer variable in the called function.
18
Passing structure by reference
19
Passing structure by reference
• Inthis program, structure variables dist1 and dist2 are passed by
value to the add function (because value of dist1 and dist2 does not
need to be displayed in main function).
• But,dist3 is passed by reference ,i.e, address of dist3 (&dist3) is
passed as an argument.
• Due to this, the structure pointer variable d3 inside the add function
points to the address of dist3 from the calling main function. So,
any change made to the d3 variable is seen in dist3 variable in main
function.
• As a result, the correct sum is displayed in the output.
20
Structure and Pointer
• Structures can be created and accessed using pointers. A pointer
variable of a structure can be created as below:
struct name {
member1;
member2;
.
.
};
int main()
{
struct name *ptr;
}
• Here, the pointer variable of type struct name is created.
21
Accessing structure's member
through pointer
• Can
be accessed by referencing pointer to another address to access
memory.
• (*personPtr).age is
same as personPtr->age
• (*personPtr).weight is same as personPtr->weight
22
Accessing structure's member
through pointer
23
Pointer to array of structures
struct Book
{
char name[10];
int price;
}
int main()
{
struct Book a; //Single structure variable
struct Book* ptr; //Pointer of Structure type
ptr = &a;
25
Pointer to
array of
structures
26
Function returning structure
• Refer the following programs:
• returning_structure.c
27
Union
• Structures allocate enough space to store all their members,
whereas unions can only hold one member value at a time.
union car
{
char name[50];
int price;
}
;
int main() {
union car car1, car2, *car3;
return 0; }
28
29