C Structure

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.

Syntax
struct structure-name{
variables,...
} structure-variables,...;
structure-name is optional and not needed if the structure variables are defined. Inside it can contain any number of variables separated by semicolons. At the end, structure-variables defines the actual names of the individual structures. Multiple structures can be defined by separating the variable names with commas. If no structure-variables are given, no variables are created. Structure-variables can be defined separately by specifying:
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.

snippet
struct address {
    int house_number;
    char street_name[50];
    int zip_code;
    char country[50];
};
Structure Definition

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.

Syntax
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.

snippet
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.

c structure memory allocation

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:

c structure
Structure Declaration

To create or declare a structure variable, there are two ways.

1st way

The first way is to declare a structure variable immediately after the structure definition, as follows.

snippet
struct address {
    int house_number;
    char street_name[50];
    int zip_code;
    char country[50];
} struct_var1, struct_var2;

2nd way

In the second way, you can declare the structure variable at a different location after structure definition. Here is structure declaration syntax:

snippet
struct address {
    int house_number;
    char street_name[50];
    int zip_code;
    char country[50];
};
struct address struct_var1, struct_var2;

Which approach is good

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.

Accessing Structure Members

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:

  1. By . (member or dot operator)
  2. By -> (structure pointer operator)

Below is the example of initialize product structure

snippet
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

snippet
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.

snippet
p1.id

C Structure example

Let's see a simple example of structure in C language.

snippet
#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;
}
Output
employee 1 id : 101 employee 1 name : John Abraham

Let's see another example of the structure in C language to store many employees information.

snippet
#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;  
}
Output
employee 1 id : 101 employee 1 name : John Abraham employee 1 salary : 56000.000000 employee 2 id : 102 employee 2 name : James Bond employee 2 salary : 126000.000000
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +