Structures in C allow you to group variable into a package. A structure is a user defined data type. It a combination of several different previously defined data types, including other structures we have defined.
struct structure-name{ variables,... } structure-variables,...;
struct structure-name new-structure-variable;
new-structure-variable will be created and has a separate instance of all the variables in structure-name.
To access a variable in the structure, you must use a record selector (.).
It should be noted that the name of structure follows the rule of variable name. Here is an example of defining address structure.
struct address { int house_number; char street_name[50]; int zip_code; char country[50]; };
C supports structure which allows us to wrap one or more variables with different data types. A structure can contain any valid data types like int, char, float, arrays or even other structures. Each variable in structure is called a structure member. To define a structure, you use struct
keyword.
struct <structure-name> { <data-type> <variable-name>; . <data-type> <array-name> [<size>]; . struct <other-struct-name>; . };
The address structure contains house number as an integer, street name as a string, zip code as an integer and country as a string.
The above example only defines an address structure without creating any structure variable.
Let's see the example to define a structure for an entity employee in c.
struct employee { int id; char name[20]; float salary; };
The following image shows the memory allocation of the structure employee that is defined in the above example.
Here, struct is the keyword; employee is the name of the structure; id, name, and salary are the members or fields of the structure. Let's understand it by the diagram given below:
To create or declare a structure variable, there are two ways.
The first way is to declare a structure variable immediately after the structure definition, as follows.
struct address { int house_number; char street_name[50]; int zip_code; char country[50]; } struct_var1, struct_var2;
In the second way, you can declare the structure variable at a different location after structure definition. Here is structure declaration syntax:
struct address { int house_number; char street_name[50]; int zip_code; char country[50]; }; struct address struct_var1, struct_var2;
If no. of variables are fixed, use 1st approach. It saves your code to declare a variable in main() function.
If number of variables are not fixed, use the 2nd approach. It provides you the flexibility to declare the structure variable many times.
C programming language treats a structure as a user-defined data type. So you can initialize a structure like a variable.
There are two ways to access structure members:
Below is the example of initialize product structure
struct product { char name[50]; double price; } book = { "C programming language", 40.5 };
In above example, we defined product structure, then we declare and initialize book structure variable with its name and price. To access structure members we can use dot operator (.) between structure variable and structure member name as follows
<Structure-Variable-Name>.<MemberAddress>
For example to access street name of structure address we do as follows
struct address myAddress; myAddress.street_name = “Srinagar”;
One of major advantage of structure is you can copy it with = operator.
struct_var1 = struct_var2;
It should be noted that both variables should be of same structure type.
Let's see the code to access the id member of p1 variable by. (member) operator.
p1.id
Let's see a simple example of structure in C language.
#include#include struct employee { int id; char name[50]; }e1; //declaring e1 variable for structure int main( ) { //store first employee information e1.id=101; strcpy(e1.name, "John Abraham");//copying string into char array //printing first employee information printf( "employee 1 id : %d\n", e1.id); printf( "employee 1 name : %s\n", e1.name); return 0; }
Let's see another example of the structure in C language to store many employees information.
#include#include struct employee { int id; char name[50]; float salary; }e1,e2; //declaring e1 and e2 variables for structure int main( ) { //store first employee information e1.id=101; strcpy(e1.name, "John Abraham");//copying string into char array e1.salary=56000; //store second employee information e2.id=102; strcpy(e2.name, "James Bond"); e2.salary=126000; //printing first employee information printf( "employee 1 id : %d\n", e1.id); printf( "employee 1 name : %s\n", e1.name); printf( "employee 1 salary : %f\n", e1.salary); //printing second employee information printf( "employee 2 id : %d\n", e2.id); printf( "employee 2 name : %s\n", e2.name); printf( "employee 2 salary : %f\n", e2.salary); return 0; }