Codzify - Structures in C Language

5 min 25 sec read Basic


Array in C does not allow to store data elements of a different kind whereas Structures allows storing data elements of a different kind. Structure in C is a user-defined data type which allows you to store data elements of a different kind.

Why Structure is used?

Suppose you want to store the information of student in a college, with attributes like Name, Roll No, Age, Gender. So, Name should be defined with a datatype "String", Age should be defined with a datatype "Integer". Here you clearly understand the difference between Arrays and Structures. In Arrays, you can only store elements of one type either Names of "String" Datatype or Ages of "Integer" Datatype whereas in Structures you can store both. Thatís the magic of structures.


Defining a Structure

The following example shows how to define a Structure in C using struct keyword:

Syntax:-

struct [structure Name]
 {
  datatype variable;
  datatype variable;
  ...
  datatype variable;
 } [one or more structure variables];

"structure Name" is an "Optional" Name which is used to define a structure.

"datatype variable" is simply declaring a normal variable int a; float per;

"[one or more structure variables]" is an "Optional" Name which is used to define one or more structure variables.


Example :-

struct Student
 {
  char name[20]; 
  int   age;
 } student; 

How to Access Structure Members?

Dot Operator (.) also known as member access operator is used to access any C structure member.

Following example clears the concept :-

#include <stdio.h>
#include <string.h>

struct Students
{
    char name[20];
    int   age;
};

int main( )
{
    /* Declare firstStudent of type Students */
    struct Students firstStudent;
    /* Declare secondStudent of type Students */
    struct Students secondStudent;
    
    /* firstStudent specification */
    strcpy( firstStudent.name, "Jazzy");
    firstStudent.age = 23;
    
    /* secondStudent specification */
    strcpy( secondStudent.name, "Steven");
    secondStudent.age = 22;
    
    /* print firstStudent info */
    printf("First Student Name: %s\n",   firstStudent.name);
    printf("First Student age: %d\n", firstStudent.age);
    
    
    /* print secondStudent info */
    printf("Second Student Name : %s\n", secondStudent.name);
    printf("Second Student age : %d\n",secondStudent.age);
    
    return 0;
}

Output :-

First Student Name: Jazzy
First Student age: 23
Second Student Name: Steven
Second Student age: 22

How to pass Structure as Function Argument?

How do we pass a normal variable to a function?
e.g: add(5,3) , swap(6,5)
Similarly, we can pass structure variables to function .

Example :-

#include <stdio.h>
#include <string.h>

struct Students {
    char  name[50];
    int  age;
 };

/* function declaration */
void display( struct Students student );

int main( ) {
    
    struct Students firstStudent;       
    struct Students secondStudent;        
    
    /* Student 1 specification */
    strcpy( firstStudent.name, "Manish");
    firstStudent.age = 23;
    
    /* Student 2 specification */
    strcpy( secondStudent.name, "Nitesh");
    secondStudent.age = 24;
    
    /* print FirstStudent info */
    display( firstStudent );
    
    /* Print Student2 info */
    display( secondStudent );
    
    return 0;
}

void display( struct Students student ) {
    
    printf( "Name : %s\n", student.name);
    printf( "Age : %d\n", student.age);
}
Output :-
Name : Manish
Age : 23
Name: Nitesh
Age : 24

How to pass Structure with Pointer Arguments?

You can define pointers to structures in the same way as you define pointer to any other variable

struct Students *pointer_Struct;

Now, you can store the address of a structure variable in the above defined pointer variable. To find the address of a structure variable, place the &; operator before the structure's name as follows

pointer_Struct = &firstStudent;

To access the members of a structure using a pointer to that structure, you must use the ? operator as follows

Example :-

pointer_Struct->name;
#include <stdio.h>
#include <string.h>

struct Students {
    char  name[50];
    int  age;
 };

/* function declaration */
void display( struct Students *student );

int main( ) {
    
    struct Students firstStudent;        
    struct Students secondStudent;        
    
    /* Student 1 specification */
    strcpy( firstStudent.name, "Manish");
    firstStudent.age = 23;
    
    /* Student 2 specification */
    strcpy( secondStudent.name, "Nitesh");
    secondStudent.age = 24;
    
    /* print FirstStudent info */
    display( &firstStudent );
    
    /* Print Student2 info */
    display( &secondStudent );
    
    return 0;
}

void display( struct Students *student ) {
    
    printf( "Name : %s\n", student->name);
    printf( "Age : %d\n", student->age);
}

Output :-
Name : Manish
Age : 23
Name : Nitesh
Age : 24

How was the tutorial? Nice. Right?



Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.



What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp


Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python