C Structures


Structure is the collection of variables of different types under a single name for better handling.


Structure Definition

Struct keyword is used to define a structure. struct define a new data type which is a collection of different type of data.

Syntax

struct structure_name 
{
    data_type member1;
    data_type member2;
    .
    .
    data_type memeber;
};

Example

struct student
{
    char name[50];
    int roll;
    float fee;
};

Here the struct student declares a structure to hold the details of student which consists of three data fields, namely name, roll and fee. These fields are called structure elements or members. Each member can have different data type,like in this case, name is of char type , roll is of int type and fee is of float type. student is the name of the structure and is called structure tag.


Structure variable declaration

It is possible to declare variables of a structure, after the structure is defined. Structure variable declaration is similar to the declaration of variables of any other data types. Structure variables can be declared in following two ways.

1) Declaring Structure variables separately

struct address
{
    char name[50];
    int cit_no;
    float salary;
};
struct address p1, p2, p[20];

2) Declaring Structure Variables with Structure definition

struct address
{
    char name[50];
    int cit_no;
}p1 ,p2 ,p[20];

Accessing members of a structure

There are two types of operators used for accessing members of a structure.

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

Any member of a structure can be accessed as: structure_variable_name.member_name

p2.cit_no

Example of structure

#include   
#include   
struct student    
{   int id;    
    char name[50];    
}s1,s2;  //declaring s1 and s2 variables for structure  
int main( )  
{  
   //store first Student information  
   s1.id=11211855;  
   //copying string into char array  
   strcpy(s1.name, "Sachin Yadav");
  
  //store second Student information  
   s2.id=11211401;  
   strcpy(s2.name, "Sanjay Kumar");  
   
   //printing first Student information  
   printf( "Student 1 id : %d\n", s1.id);  
   printf( "Student 1 name : %s\n", s1.name);  
  
   //printing second Student information  
   printf( "Student 2 id : %d\n", s2.id);  
   printf( "Student 2 name : %s\n", s2.name);  
  
   return 0;  
}  
Output
Student 1 id : 11211855
Student 1 name : Sachin Yadav
Student 2 id : 11211401
Student 2 name : Sanjay Kumar

Nested Structures

Structures can be nested within other structures.
struct student
{
 char[30] name;
 int age;
   struct address
    {
     char[50] locality;
     char[50] city;
     int pincode;		
    };
}; 

Structures as Function Arguments

We can pass a structure as a function argument in similar way as we pass any other variable or array.

#include <stdio.h>
#include <conio.h>
struct student
{
 char name[10];
 int roll;
};
void show(struct student st);
void main()
{
 struct student std;
 clrscr();
 printf("\nEnter student record\n");
 printf("\nstudent name\t");
 scanf("%s",std.name);
 printf("\nEnter student roll\t");
 scanf("%d",&std.roll);
 show(std);
 getch();
}

void show(struct student st)
{
 printf("\nstudent name is %s",st.name);
 printf("\nroll is %d",st.roll);
}

Share this article on