Skip to main content
Submitted by admin on 26 March 2022

Additional info: A structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type.

In C programming language, a structure is a user-defined data type that groups together variables of different data types under a single name. Structures are useful when we need to store and manipulate related data items as a single unit.

Here is an example of a structure defination in C:

structure defination

	  
	  struct student { 
	  char name[50];
	  int age;
	  float marks;
	  };
	  
	

In the above example, we have defined a structure named student that has three members - name, age, and marks.

name is a character array of size 50, age is an integer, and marks is a float.

structure declaration

We can declare variables of this structure type as follows:

		
		struct student s1;
		
	

This creates a variable s1 of type student.

access the members of a structure

We can access the members of a structure using the dot operator (.) as follows:

      
	  strcpy(s1.name, "John");
	  s1.age = 20;
	  s1.marks = 85.5;
	  
   

This sets the values of the members name, age, and marks of the variable s1.

We can also initialize a structure at the time of declaration as follows:

      
	  struct student s1 = {"John", 20, 85.5};
	  
   

Benefit

memory consumption
No memory consumed for definition of the structure until declared.

Rule

  1. You have to prefix struct keyword while using structure variable
  2. structure variable consume 6 byte memory for storage.

We can pass a structure to a function by value or by reference.

passed by value
When passed by value, a copy of the structure is passed to the function.
      
	  // passing by value
	  void display(struct student s) { 
	  printf("Name: %s\n", s.name); 
	  printf("Age: %d\n", s.age); 
	  printf("Marks: %.2f\n", s.marks);
	  }
	  
	  void main() { 
	  struct student s1 = {"John", 20, 85.5};
	  display(s1);
	  }
	  
	

In the above example, we have defined a function display that takes a struct student parameter s. We have also defined a variable s1 of type struct student and initialized it. We then call the function display and pass s1 as a parameter. The function displays the values of the members of s1.

passed by reference
When passed by reference, a pointer to the structure is passed to the function.

Structure is similar to an array but the only difference is that array is collection of similar data type but structure can be collection of similar or different data type. Each variable declared inside structure is called member of structure

Structures in C++ can contain two types of members:

Variables: We can create a structure with variables of different data types in C++.

Functions: Normal C++ functions. Along with variables, we can also include functions inside a structure declaration.

Structure declaration

The ‘struct’ keyword is used to create a structure followed by the structure name and structure's member variables are declared within braces.

struct structureName{

    member1;

    member2;

    member3;

    .

    .

    .

    memberN;

};

 

Accessing the structure members:  Two ways 

 

1.using the dot operator(.)

We have to create an object of structure to access its members. Object is a variable of type structure. Structure members are accessed using the dot operator(.) between structure's object and structure's member name.

Syntax for creating object

struct struct-name obj;

2.using the operator(->)

How to declare structure variables?

 

A structure variable(In two ways) can either be declared with structure declaration or as a separate declaration like basic data types.

 

// A variable declaration with structure declaration. 

struct structureName{

    member1;

    member2;

    member3;

    .

    .

    .

    memberN;

}structureVariable ;  

// The variable structureVariable is declared with structure  'structureName

  

  

// A variable declaration as separate declaration like basic data types 

struct structureName{

    member1;

    member2;

    member3;

    .

    .

    .

    memberN;

}; 

  

int main() 

   struct structureName structureVariable;  // The variable structureVariable is declared like a normal variable 

}

Note: In C++, the struct keyword is optional before in declaration of a variable. But In C, it is mandatory.

 

Passing and Return Structure to and from Function

Similar to variables of built-in types, you can also pass structure variables to a function. structure can pass into function as function argument and we can also return structure from function.

Structure can be passed to function through its object therefore passing structure to function or passing structure object to function is same thing because structure object represents the structure.

 Like normal variable, structure variable(structure object) can be pass by value or by references / addresses.

 

passing structure  by value









 

 

       struct Employee

       {

              int Id;

              char Name[25];

              int Age;

              long Salary;

       };

 

       void Display(struct Employee);

       int main()

       {

              struct Employee Emp = {1,"Kumar",29,45000};

 

              Display(Emp);

 

       }

 

       void Display(struct Employee E)

       {

                    printf("\n\nEmployee Id : %d",E.Id);

                    printf("\nEmployee Name : %s",E.Name);

                    printf("\nEmployee Age : %d",E.Age);

                    printf("\nEmployee Salary : %ld",E.Salary);

       }

 

   Output :

 

              Employee Id : 1

              Employee Name : Kumar

              Employee Age : 29

              Employee Salary : 45000

 

passing structure  by reference/address

 

 

       struct Employee

       {

              int Id;

              char Name[25];

              int Age;

              long Salary;

       };

 

       void Display(struct Employee*);

       int main()

       {

              struct Employee Emp = {1,"Kumar",29,45000};

 

              Display(&Emp);

 

       }

 

       void Display(struct Employee *E)

       {

                    printf("\n\nEmployee Id : %d",E->Id);

                    printf("\nEmployee Name : %s",E->Name);

                    printf("\nEmployee Age : %d",E->Age);

                    printf("\nEmployee Salary : %ld",E->Salary);

       }

 

   Output :

 

              Employee Id : 1

              Employee Name : Kumar

              Employee Age : 29

              Employee Salary : 45000

Frequently Asked Questions

Q1. Where to use the Structure data type?
We can use this data type to store data of different attributes of different data types. For example, If we want to store data on multiple patients such as patient name, age, and blood group.
Q2. How to create a structure?
‘struct’ keyword is used to create a structure. Following is an example. struct address { char name[50]; char street[100]; char city[50]; char state[20]; int pin; };
Q3. How to declare structure variables?
A structure variable can either be declared with structure declaration or as a separate declaration like basic types. // A variable declaration with structure declaration. struct Point { int x, y; } p1; // The variable p1 is declared with 'Point' // A variable declaration like basic data types struct Point { int x, y; }; int main() { struct Point p1; // The variable p1 is declared like a normal variable }
Q4. How to initialize structure members?
Structure members can be initialized using curly braces ‘{}’. For example, the following is a valid initialization. struct Point { int x, y; }; int main() { // A valid initialization. member x gets value 0 and y // gets value 1. The order of declaration is followed. struct Point p1 = {0, 1}; } Structure members cannot be initialized with declaration. For example, the following C program fails in the compilation. struct Point { int x = 0; // COMPILER ERROR: cannot initialize members here int y = 0; // COMPILER ERROR: cannot initialize members here }; The reason for above error is simple, when a datatype is declared, no memory is allocated for it. Memory is allocated only when variables are created. Structure members can be initialized using curly braces ‘{}’. For example, the following is a valid initialization.
Q5. How to access structure elements?
Structure members are accessed using dot (.) operator. #include  struct Point { int x, y; }; int main() { struct Point p1 = { 0, 1 }; // Accessing members of point p1 p1.x = 20; printf("x = %d, y = %d", p1.x, p1.y); return 0; }
Q5. What is designated Initialization?
Designated Initialization allows structure members to be initialized in any order. #include  struct Point { int x, y, z; }; int main() { // Examples of initialization using designated // initialization struct Point p1 = { .y = 0, .z = 1, .x = 2 }; struct Point p2 = { .x = 20 }; printf("x = %d, y = %d, z = %d\n", p1.x, p1.y, p1.z); printf("x = %d", p2.x); return 0; }
Q6. What is an array of structures?
Like other data types, we can create an array of structures. #include  struct Point { int x, y; }; int main() { // Create an array of structures struct Point arr[10]; // Access array members arr[0].x = 10; arr[0].y = 20; printf("%d %d", arr[0].x, arr[0].y); return 0; }
Q7. What is a structure pointer?
Like primitive types, we can have a pointer to a structure. If we have a pointer to structure, members are accessed using arrow ( -> ) operator.. #include  struct Point { int x, y; }; int main() { struct Point p1 = { 1, 2 }; // p2 is a pointer to structure p1 struct Point* p2 = &p1; // Accessing structure members using structure pointer printf("%d %d", p2->x, p2->y); return 0; }