Declaration and Initialization of structure in c

5 min 25 sec read Basic


Array in C does not allow storing 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 that allows you to store data elements of a different kind.

 

Why Structure is used?

 

Suppose you want to store the information of students in a college, with attributes like Name, Roll No, Age, and Gender. So, Name should be defined with a datatype "String", and 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.


Declaration of structure in c 

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

Syntax:-

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

"structure Name" is an "Optional" Name that 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 that is used to define one or more structure variables.


Example:-

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

 

Accessing structure members in c 

 

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

The 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
",   firstStudent.name);
    printf("First Student age: %d
", firstStudent.age);
    
    
    /* print secondStudent info */
    printf("Second Student Name : %s
", secondStudent.name);
    printf("Second Student age : %d
",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 
#include 

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
", student.name);
    printf( "Age : %d
", student.age);
}

Output :-

Name : Manish
Age : 23
Name: Nitesh
Age : 24

How to pass struct by reference in c?

To pass a struct by reference in C, You can define pointers to structures in the same way as you define pointers 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
", student->name);
    printf( "Age : %d
", student->age);
}

Output:-

Name : Manish
Age : 23
Name : Nitesh
Age : 24

 

Codzify's

1:1 & 1:Many Mentorship Program

Be job-ready. Learn how to crack the interviews with industry ready training program.


Learn More

Data Structures & Algorithms

What is Dynamic Programming and how to use it

This is a quick introduction to Dynamic Programming.

3 ways to check anagrams

Let's discuss the 3 ways to check anagrams and their time complexity.

Bubble Sort Algorithm

Topics covered: 1) Basics of Bubble Sort Algorithm 2) Worst Case, Best Case complexity

Introduction to Trees

What is a Tree and Terminologies like root, sibling, parent node, children, Leaf node, Ancestor & Descendant, Properties of Tree Data Structure, Depth of node, Height of node

Address Calculation in 2d array

Let's discuss about the Address Calculation using row-major order & column-major order.

Javascript

Javascript - Postfix Vs Prefix

Let's discuss the difference between postfix and prefix with a practical example.

Javascript Identifiers, Variables

In this session, we will discuss the concept of identifiers, variables and operators in javascript with a practical example.

Bootstrap

Bootstrap 4 Tutorial: Modal Popups Made Easy | Live Demo

Let's discuss the Modal Popups in Bootstrap.

How to create a dropdown list in navbar bootstrap

In this video, we will discuss How to create a dropdown list in navbar bootstrap with a practical example.

Navigation tabs in Bootstrap

Let's discuss about the navigation tabs in Bootstrap

Bootstrap Buttons

Let's discuss about the button classes in bootstrap.


Recommended tutorials