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 |
In C language, an array is used to collect same data type values under a single variable name at contiguous memory locations but what happens if we need to collect different-2 datatype Values under the same roof. For example, you want to maintain the attendance record of employees of any company. Attendance record of an employee will contain following attributes
name 2. employeId 3. attendance
All above attributes of record need different-2 data type as shown in below table.
Information | DataType |
---|---|
Name | string(Array of char dataType) |
employeId | unsigned int |
Salary | double |
So as we can see above all the members have the different-2 data type and if we initialize these individually then we can never get the right information of any employee because we don’t know where the requested field has stored. so in this situation, C gives a unique data type that will keep all the members [of same data type or heterogeneous data type] under the master data type which is called ‘struct‘ and concept is known as ‘structure’.
so ‘struct’ data type is secondary data type which is the collection of homogeneous or heterogeneous data types.