Unlike an array, Structure is the collection of heterogeneous data type elements.
A structure is a secondary data type.unlike an array, it is used to collect different-2 data types element.
1 2 3 4 5 6 7 |
struct tag_name[optionl]{ dataType member1; dataType member2; ................; ................; dataType memebrn; }structure_variable_Name; |
where tag_name is optional.
structure_variable_Name is identifier through which you can access any member with dot(.) operator.
Requirement: You want to save information of an employee in memory.
Employee Information:
Information | DataType |
---|---|
Name | string(Array of char dataType) |
employeId | unsigned int |
Salary | double |
You have two option to save this info in memory –
— Declare the separate variables for each info.
— Use structure concept and store all heterogeneous info with a single identifier.
— I will prefer the second choice for smart programming-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
struct empInfo { unsigned char name[20]; unsigned int empId; double salary; }emp; OR struct empInfo { unsigned char name[20]; unsigned int empId; double salary; }; struct empInfo emp; |
empInfo: structure tag or name
emp: structure empInfo type variable.
dot(.) operator is used by the structure identifier to access the members of a structure.
— emp.name=const_value1
— emp.empId=const_value2
— emp.salary=const_value3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
struct empInfo { unsigned char name[20]; unsigned int empId; double salary; }emp; OR struct empInfo { unsigned char name[20]; unsigned int empId; double salary; }; struct empInfo *emp; |
— emp->name=const_value1
— emp->empId=const_value2
— emp->salary=const_value3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> struct empInfo { unsigned char name[20]; unsigned int empId; double salary; }; int main() { struct empInfo emp={"John",12345,89999.466}; printf("Size of structure empInfo is =%lu\n",sizeof(struct empInfo)); printf("Value at structure members are :\n"); printf("emp.name=%s\n",emp.name); printf("emp.empId=%u\n",emp.empId); printf("emp.salary=%lf\n",emp.salary); return 0; } |
Output:
1 2 3 4 5 |
Size of structure empInfo is =32 Value at structure members are : emp.name=John emp.empId=12345 emp.salary=89999.466000 |
typedef is used to give an alias name to the existing data type.Through the use of typedef, we can assign an alias name to the existing struct data type.
1 2 3 4 5 6 7 8 9 10 |
typedef struct { unsigned char name[20]; unsigned int empId; double salary; }empInfo; /* now empinfo is the alias name for struct i.e struct = empInfo*/ empInfo emp; |
Let’s apply typedef in above example –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <stdio.h> #include<string.h> typedef struct { unsigned char name[20]; unsigned int empId; double salary; }empInfo; /* now empinfo is the alias name for struct i.e struct = empInfo*/ int main() { empInfo emp={"John",12345,89999.466}; /*empInfo is the alias name of struct*/ printf("Size of structure empInfo is =%lu\n",sizeof(empInfo)); printf("Value at structure members are :\n"); printf("emp.name=%s\n",emp.name); printf("emp.empId=%u\n",emp.empId); printf("emp.salary=%lf\n",emp.salary); return 0; } |
Output
1 2 3 4 5 |
Size of structure empInfo is =32 Value at structure memebrs are : emp.name=John emp.empId=12345 emp.salary=89999.466000 |
Nesting of structure means use of structure inside a structure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
typedef struct { unsigned char date; unsigned char month; unsigned char year; }empBday; struct empInfo { unsigned char name[20]; empBday bday; /*nested structure*/ unsigned int empId; double salary; }; |
In above structure empBday which is itself a structure is the member of another structure named empInfo .This process is called nesting of structure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <stdio.h> typedef struct { unsigned char date; unsigned char month; unsigned char year; }empBday; struct empInfo { unsigned char name[20]; empBday bday; /*nested structure*/ unsigned int empId; double salary; }; int main() { struct empInfo emp={"Amit",{18,10,88},3800209,80000}; printf("Name=%s\t",emp.name); printf("Bday=%u-%u-%u\t",emp.bday.date,emp.bday.month,emp.bday.year); printf("empId=%u\t",emp.empId); printf("Salary=%lf\n",emp.salary); return 0; } |
Output
1 |
Name=Amit Bday=18-10-88 empId=3800209 Salary=80000.000000 |