Introduction

Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Unlike an array, a structure can contain many different data types (int, float, char, etc.).

Now you must be thinking that how can we make a structure.So, let’s see:

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

Declaring Structure Variable

A structure variable can either be declared with a 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
}

write your code here: Coding Playground

Now you must be thinking that how can we initialize a structure variable. So here is the answer:

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

The reason for the above error is simple when a datatype is declared, no memory is allocated for it. Memory is allocated only when variables are created.

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

Accessing Members of the Structure

There are two ways to access structure members:

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

Let’s see one example:

#include<stdio.h> 
#include <string.h>   
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, "Sonoo Jaiswal");//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;   
}    

write your code here: Coding Playground

Limitations of Structures

  • The C structure does not allow the struct data type to be treated like built-in data types.
  • We cannot use operators like +,- etc. on Structure variables.
  • No Data Hiding: C Structures do not permit data hiding. Structure members can be accessed by any function, anywhere in the scope of the Structure.
  • Functions inside Structure: C structures do not permit functions inside the Structure
  • Static Members: C Structures cannot have static members inside their body
  • Access Modifiers: C Programming language does not support access modifiers. So they cannot be used in C Structures.
  • Construction creation in Structure: Structures in C cannot have a constructor inside Structures.