Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
Define structures
3.
Declaring structure variables
3.1.
Using the structure tag 
3.2.
With structure declaration
4.
Accessing Members of a Structure
5.
Initialization of structure variables
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024

Introduction to Structure

Author Shivam Verma
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction 

Structure is another user-defined data type available in C that allows us to combine data items of different kinds. The structure is similar to an array; the only difference is array is used to store the same data types. But, on the other hand, the structure is used to store data of any type, which is practical more useful. For example, if we want to create a record of a student record that contains the name, marks(%), and roll number of that student, we can not use an array because all three data elements are of different types. To store these related fields of different data types, we can use a structure that is capable of storing heterogeneous data. 

Also See, Sum of Digits in C, and  Tribonacci Series

Define structures

We use struct keyword to define the structure. The syntax of a structure declaration in C is-

struct structure_name   
{  
    datatype member1;  
    datatype member2;  
    .  
    .  
    datatype memberN;  
};

In the above syntax, struct is a keyword, member1, member2, …….., memberN are members of the structure and are declared inside curly braces. These members can be of any data type like char, int, float, array, pointers, or another structure type. 

Example 

struct student  
{
    char name[20];  
    float marks;
    int rollno;
};

In the above example, the structure named student has been defined, which contains three elements named name, marks, and rollno.

You can also read about the dynamic arrays in c and Short int in C Programming

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Declaring structure variables

We declare a variable for the structure to easily access the member of the structure. There are the following ways to declare structure variables:

  1. Using the structure tag 
  2. With structure declaration

Using the structure tag 

Here is an example of declaring the structure variables by struct keyword. 

struct student  
{
    char name[20];  
    float marks;
    int rollno;
};
struct student stu1, stu2, stu3;   //declaring variables of struct Student

In the above example, stu1, stu2, and stu3 are structure variables that are declared using structure tag student.

With structure declaration

Here is another way to declare variables at the time of defining the structure.

struct student  
{
    char name[20];  
    float marks;
    int roll_no;
}stu1, stu2, stu3;

In the above example, stu1, stu2, and stu3 are variables of type struct student. If the number of variables is not fixed, use the 1st approach; otherwise, use the 2nd approach.

Accessing Members of a Structure

There are two ways to access any member of a structure variable:

  1. Using  . (dot operator)
  2. Using -> (structure pointer operator)

Generally, we use the member access operator (.) to access any member of a structure. The format for accessing a structure member is-

struct_variable.member

In the above syntax, on the left side of the dot operator, there should be a variable of structure type, and on the right side, there should be the name of a member of that structure.

For example

#include<stdio.h>
#include<string.h>
struct student
{
    char name[20];
    int rollno;
    float marks;
};
int main()
{
    struct student stu1;
    stu1.rollno=12;
    strcpy(stu1.name, "Dheeraj");
    //displaying the stored values
    printf("Name of student 1: %s\n", stu1.name);
    printf("Roll number of student 1: %d\n", stu1.rollno);
    return 0;
}

Output

Name of student 1: Dheeraj
Roll number of student 1: 12

In the above example, structure variable stu1 contains 'Dheeraj' for its first element name and 12 for its second element rollno.

Initialization of structure variables

The syntax of initializing structure variables is similar to that of arrays. We can use curly braces to assign initial values to a structure variable. The number, order, and type of these values should be the same as in the structure template definition.

Syntax: 

struct <tagname><structvariable>={List of values};

Example 

struct student
{
    char name[20];
    int rollno;
    float marks;
};
struct student stu={"Dheeraj",12,78.9};

In the above example, structure variable stu1 has been initialized with values "Dheeraj", 12, and 78.9. "Dheeraj" would be stored in stu.name, and 12 would be stored in stu.rollno, and 78.9 would be stored in stu.marks.

Here is a C to Program to demonstrate the initialization of structure variables.

#include<stdio.h>
#include<string.h>
struct student
{
    char name[20];
    int rollno;
    float marks;
};
int main()
{
    struct student stu={"Dheeraj",12,78.9};
    //displaying the stored values
    printf("Name of student: %s\n", stu.name);
    printf("Roll number of student: %d\n", stu.rollno);
    return 0;
}

Output

Name of student: Dheeraj
Roll number of student: 12

FAQs

  1. What is an array of structures?
    Ans: An array of structures C programming is simply an array in which each element is a structure of the same type.
     
  2. What is pointer to structure in C?
    Ans: Pointer to structure holds the add of the entire structure. We use it to create complex data structures such as linked lists, trees, graphs, etc. The members of the structure can be accessed using a special operator called an arrow operator ( -> ).
     
  3. Can we declare a function inside structure of C Programming?
    Ans: No, We can not declare a function inside structure of C Programming.
     
  4. Can we use access specifiers(private, public, protected) with structures?
    Ans: Yes, We can use access specifiers(private, public, protected) with structures.

Key Takeaways

In this blog, We learned about structure. This blog also discussed the declaration of structure variables, accessing of a structure, and initialization of structure variables.

This blog is over, but the thirst for learning is not over yet. To understand the difference between classes and structure, click here. You can use our practice platform Coding Ninjas Studio to practice various DSA questions asked in many interviews.
Check out this problem - Redundant Braces

Next article
Structures and Functions
Live masterclass