Structures in C language are basically user-defined data types that enables the user to create a data type that can group elements of different data types into it.

Thus, it contains different data types to be represented by a single Structure name.


Creating Structures in C

C language uses the struct keyword to create a structure.

Syntax:

struct Structure_Name
{
    Datatype data_member1;
    Datatype data_member2;
    .
    .
    Datatype data_memberN;
};

As mentioned above, C language uses the struct keyword to build a structure. Inside the curly brackets, the user can define the data members necessary to serve the purpose of the particular program. These data members are the basic C language variables of different data types such as int, float, double, char, etc.

It is mandatory to add a semi-colon (;) after the closing curly bracket of a particular structure.

Example:

struct Student_info
{
char name[100];
char address[100];
char division[50];
int roll_num;
};

In the above code snippet, we have created a structure as Student_info to serve the purpose of Student Information. Within the structure, we have declared data members: name, address, division, and roll_num with their particular data type.


Declaration of Structure Variables

Structure Variables enable the user to access the data-members declared inside the structure.

Either of the following ways can be used to declare Structure Variables in C:

  • Declaration of Structure variables after the Structure definition
  • Declaration of Structure variables along with the Structure definition

1. Declaration of Structure variables after the Structure definition

Syntax:

struct Structure_Name
{
    Datatype data_member1;
    Datatype data_member2;
    .
    .
    Datatype data_memberN;
};
struct Structure_Name Variable1, Variable2,.., VariableN;

Example:

struct Student_info
{
char name[100];
char address[100];
char division[50];
int roll_num;
};
struct Student_info S1, S2;

In the above snippet of code, we have created Structure Variables S1 and S2 for the Structure Student_info after the declaration of the structure.

2. Declaration of Structure variables along with the Structure definition

Syntax:

struct Structure_Name
{
    Datatype data_member1;
    Datatype data_member2;
    .
    .
    Datatype data_memberN;
} Structure_variable1, Structure_variable2;

Example:

struct Student_info
{
char name[100];
char address[100];
char division[50];
int roll_num;
}S1, S2;

In the above piece of code, we have created the Structure Variables S1 and S2 along with the declaration of the structure Student_info.


Initializing Structure Data Members

The C Structure data members cannot be initialized while declaring them. They need to be separately assigned values after the declaration of data members of the structure.

Example: Declaring Structure Data members while declaring them gives an error

struct Student_info
{
char *name = "Safa"; // This statement gives compilation error
};

As mentioned above, the Structure data members need to be initialized separately after the declaration.

Syntax:

Structure_Name.Structure_data_member_name = Value

Accessing data members in a Structure

C uses the period or member access operator (.) to access the data members of the structure.

Syntax:

Structure_variable_name.Structure_data_member_name

Example:

#include <stdio.h>
struct Student_info
{
char *name;
char *address;
char *division;
int roll_num;
};
int main()
{
     struct Student_info S1;
     S1.name = "Safa Mulani";
     S1.address = "Pune";
     S1.division = "A";
     S1.roll_num = 24105;
     printf("The Student Information is as follows:n");
     printf("nName: %s", S1.name);
     printf("nAddress: %s", S1.address);
     printf("nDivision: %s", S1.division);
     printf("nRoll Number: %d", S1.roll_num);
     return 0;
}

Here, we have created variable S1 of type struct. Further, we have accessed the structure variables: name, address, division and roll_num using S1.

Output:

The Student Information is as follows:
Name: Safa Mulani
Address: Pune
Division: A
Roll Number: 24105

Array of Structure

C allows the users to use an array to represent the data elements of the Structure.

Syntax:

struct Structure_Name array_name[size];

In the below snippet of code, we have created an array ā€˜S’ of structure Student_info with size = 3.

Example:

#include<stdio.h>
struct Student_info
{
char name[100];
char division[50];
int roll_num;
};
void main()
{
struct Student_info S[3];
for(int i = 0; i < 3; i++)
    {
        printf("nEnter Student Details:n");
        printf("nName:t");
        scanf("%s", S[i].name);
        printf("nDivision:t");
        scanf("%s", S[i].division);
        printf("nRoll Number:t");
        scanf("%d", &S[i].roll_num);
    }
    printf("nStudent Information:n");
    for(int i = 0; i < 3; i++)
    {
        printf("nName: %s", S[i].name);
        printf("nRoll Number: %d", S[i].roll_num);
        printf("nDivision: %s", S[i].division);
    }
}

Output:

Enter Student Details:
Name: Safa
Division: A
Roll Number: 23105
Enter Student Details:
Name: Aman
Division: B
Roll Number:21042
Enter Student Details:
Name: Divya
Division: D
Roll Number: 2134
Student Information:
Name: Safa
Roll Number: 23105
Division: A
Name: Aman
Roll Number: 21042
Division: B
Name: Divya
Roll Number: 2134
Division: D

Structure as Function Parameters

C enables programmers to pass a Structure as an argument to a function in a similar fashion of passing variables/arrays as arguments to functions.

Example:

#include <stdio.h>
struct Evaluation
{
    char name[50];
    int score;
};
void display_details(struct Evaluation e1);
int main()
{
    struct Evaluation e;
    printf("Name: ");
    scanf("%[^n]%*c", e.name);
    printf("Score: ");
    scanf("%d", &e.score);
    display_details(e);   // passing structure as an argument
    return 0;
}
void display_details(struct Evaluation e1)
{
  printf("nEvaluation details.....n");
  printf("Name: %s", e1.name);
  printf("nScore: %d", e1.score);
}

Output:

Name: Safa
Score: 56
Evaluation details.....
Name: Safa
Score: 56

Nested Structures

Nested Structures in C basically defining one structure into another structure i.e. it permits one structure to have another structure as a variable.

Example:

struct Student_info
{
char *name;
char *Branch_name;
struct Evaluation
    {   char *division;
        int hsc_score;
        int ssc_score;
        int roll_num;
    }eval;
}student;

In the above snippet, We have created a structure Student_info wherein we have represented another structure Evaluation as a member of it.

You can access the nested structs using struct1_var.struct2_var.struct2_innervar

For example, in the above example, we used student as struct 1 variable and eval for the nested struct. We can access the inner variables as student.eval.ssc_score;


Pointers in Structure

C allows us to have a pointer to a structure. The arrow (->) operator enables the user to access the data members of the structure having a pointer to it.

Syntax:

struct Structure_Name
{
    Datatype data_member1;
    Datatype data_member2;
    .
    .
    Datatype data_memberN;
};
int main()
{
    struct Structure_Name *pointer_name;
}

Example:

#include<stdio.h>
struct Student_info
{
char *name;
int roll_num;
};
int main()
{
struct Student_info *St, S;
St = &S;
        printf("nEnter Student Details:n");
        printf("nName:t");
        scanf("%s", St->name);
        printf("nRoll Number:t");
        scanf("%d", &St-&gt;roll_num);
        printf("nStudent Information:n");
        printf("nName: %s", St->name);
        printf("nRoll Number: %d", St->roll_num);
        return 0;
}

Output:

Enter Student Details:
Name:	Safa
Roll Number:	1234
Student Information:
Name: Safa
Roll Number: 1234

Conclusion

Thus, in this article, we have understood the concept of Structures in C language.


References

By admin

Leave a Reply

%d bloggers like this: