Structures in C

What is a structure?
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.

 
How to create a structure?
‘struct’ keyword is used to create a structure. Following is an example.

filter_none

edit
close

play_arrow

link
brightness_4
code

struct address
{
   char name[50];
   char street[100];
   char city[50];
   char state[20];
   int pin;
};

chevron_right


 
How to declare structure variables?
A structure variable can either be declared with structure declaration or as a separate declaration like basic types.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
}

chevron_right


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



How to initialize structure members?
Structure members cannot be initialized with declaration. For example the following C program fails in compilation.

filter_none

edit
close

play_arrow

link
brightness_4
code

struct Point
{
   int x = 0;  // COMPILER ERROR:  cannot initialize members here
   int y = 0;  // COMPILER ERROR:  cannot initialize members here
}; 

chevron_right


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, following is a valid initialization.

filter_none

edit
close

play_arrow

link
brightness_4
code

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}; 
}

chevron_right


 
How to access structure elements?
Structure members are accessed using dot (.) operator.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
  
struct Point
{
   int x, y;
};
  
int main()
{
   struct Point p1 = {0, 1};
  
   // Accesing members of point p1
   p1.x = 20;
   printf ("x = %d, y = %d", p1.x, p1.y);
  
   return 0;
}

chevron_right


Output:

x = 20, y = 1

 
What is designated Initialization?
Designated Initialization allows structure members to be initialized in any order. This feature has been added in C99 standard.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
  
struct Point
{
   int x, y, z;
};
  
int main()
{
   // Examples of initializtion 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;
}

chevron_right


Output:

x = 2, y = 0, z = 1
x = 20

This feature is not available in C++ and works only in C.

 
What is an array of structures?
Like other primitive data types, we can create an array of structures.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
  
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;
}

chevron_right


Output:

10 20

 
What is a structure pointer?
Like primitive types, we can have pointer to a structure. If we have a pointer to structure, members are accessed using arrow ( -> ) operator.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
  
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;
}

chevron_right


Output:

1 2

What is structure member alignment?
See https://www.geeksforgeeks.org/structure-member-alignment-padding-and-data-packing/

We will soon be discussing union and other struct related topics in C. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up

Improved By : RajeetGoyal, mxp, RishabhPrabhu