Structures in C

The structure is a user-defined data structure that is used to bind two or more data types or data structures together. Like for storing details of a student, we can create a structure for student that has the following data types: character array for storing name, an integer for storing roll number, and a character for storing section, etc. Structures don’t take up any space in the memory unless and until we define some variables for it. When we define its variables, they take some memory space which depends upon the type of the data member and alignment (discussed below).

How to Create a Structure?

To create a structure in C, the struct keyword is used followed by the tag name of the structure. Then the body of the structure is defined, in which the required data members (primitive or user-defined data types) are added.

Syntax:

struct structure_name
{
Data_member_type data_member_defination;
Data_member_type data_member_defination;
Data_member_type data_member_defination;
…
…
}(structure_variables);struct Student
{
char name[50];
int class;
int roll_no;
} student1;

In the above syntax, the data_members can be of any data type like int, char, double, array or even any other user-defined data type. The data_member_definition for the data types like character array, int, double is just a variable name like name, class, and roll_no. We have also declared a variable, i.e., student1 of the Student structure. Please note that it is not mandatory to always declare structure variables in this way. We will see other ways in the coming sections.

How to Declare Structure Variables?

If we have created a Student structure for storing data of students with all the data members like student name, student class and student section, how can we use them? To use the properties of the created structure in C, we have to create structure variables.

Syntax:

struct structure_name {
// body of structure
} variables;

struct Student {
char name[50];
int class;
int roll_no;
} student1; // here 'student1' is a structure variable of type, Student.

Description of the Syntax :

  • Keyword struct: The keyword struct is used at the beginning while defining a structure in C. Similar to a union, a structure also starts with a keyword.
  • structName: This is the name of the structure which is specified after the keyword struct.
  • data_Type: The data type indicates the type of the data members of the structure. A structure can have data members of different data types.
  • member_name: This is the name of the data member of the structure. Any number of data members can be defined inside a structure. Each data member is allocated a separate space in the memory.

Accessing Structure Members :

Structure members can be accessed and assigned values in a number of ways. Structure members have no meaning individually without the structure. In order to assign a value to any structure member, the member name must be linked with the structure variable using a dot . operator also called period or member access operator.

For Examples:-

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

struct Student
{
    char name[20];
    int age;
    char branch[10];
    //F for female and M for male
    char gender;
};

int main()
{
    struct Student s1;
    
    /*
        s1 is a variable of Student type and 
        age is a member of Student
    */
    s1.age = 25;
    /*
        using string function to add name
    */
    strcpy(s1.name, "Peter");
    /*
        displaying the stored values
    */
    printf("Name of Student 1: %s\n", s1.name);
    printf("Age of Student 1: %d\n", s1.age);
    
    return 0;
}

Output :

Name of Student 1: Peter
Age of Student 1: 25

Leave a Comment

Your email address will not be published. Required fields are marked *

// Sticky ads