Structure in C Programming

Introduction

  • Structure is a collection of data items of different data types.
  • It is a user defined data type which stores related information together.
  • It is also know as a collection of variables under a single name.

Defining a data structure

General format of a structure is as follows:
struct struct_name
{
     member 1;
     member 2;
     .
     .
     member n;
};

  • The semicolon should be used after the closing brace brackets.
  • struct is the required keyword.
  • struct_name is the name given to the structure.

Example : Definition of a structure

struct student
{
    char name[20];
    int roll_no;
    float marks, avg;
};


Other than this we can even combine the variable declaration with the structures.

It is defined in the following format:
struct struct_name
{
     member 1;
     member 2;
     .
     .
     member n;
} var1, var2, …..varn;

Example

struct student
{
    char name[20];
    int roll_no;
    float marks, avg;
}s1, s2, s3;

typedef declarations

  • By using the typedef declaration a new name can be assigned to an existing data type or a complex data type.
  • It is a user defined data type.
Syntax:
typedef existing data-type new data-type

Example : typedef declaration
typedef int x;

  • The new data-type is not established but it is a new name given to the existing data-type.
  • It is generally used by structures as it eliminates the repeated use of struct tag while defining the variables.
  • As it is a new name it can also suggest the purpose of the structure.

Example : Using typedef declaration

typedef struct student
{
    char name[20];
    int roll_no;
    float marks, avg;
} record;


record is used instead of using s1, s2, s3.

Structure initialization

  • Initializing a structure means some constants are assigned to the members of the structure.
  • An error can occur if we assign a structure of one type to a structure of another type.
  • While initializing, the values should be given in the order which the members are set.

Syntax

struct struct_name
{
    member 1;
    member 1;
    .
    .
    member n;
}struct_var = {constant1,constant2,....constantn};

OR

struct struct_name
{
     member 1;
     member 1;
     .
     .
     member n;
};
struct struct_name struct_var = {constant1,constant2,....constantn};


Example : Initialization of a structure

struct student
{
    char name[20];
    int roll_no;
    float fees;
}s1={“Raj”, 5, 50000};

OR

struct student s1 = {“Raj”, 5, 50000};

Accessing the members of the structure

Syntax:
structure variable.member

  • The '.' operator will select the particular member of the structure. It has the highest precedence and its associativity is from left to right.
  • The members of the structure can be accessed on an individual basis.

  • Example : Accessing members
    s1.name = “Raj”;
    s1.roll_no = 5;
    s1.fees = 50000;

  • In the above example, you can see that 's1' is accessed.
  • Memory is allocated when the structure is instantiated.
  • Once we know how the members are accessed they can be used like any other normal variable.

Nested Structures

  • A structure which contains another structure as its member is called a nested structure.
  • It is required when a structure has to broken down into entities so that the members can be declared as structure.

  • Example : To add the first name, middle name and last name of the student

    struct name
    {
        char first_name[20];
        char middle_name[20];
        char last_name[20];
    };
    struct student
    {
        char name[20];
        int roll_no;
        int fees;
        struct name n;
    }s1;


  • Member 'n' is a structure itself. Here, the declaration of the embedded structure should be done before the declaration of the outer structure.

  • They can be initialized in the following manner:
    struct student s1={“Raj”, “Sandeep”, “Sharma”, 5, 50000};

  • This will append the members of the embedded structure to the main structure.
  • The members of the embedded structure can be obtained by using the dot operator. If we want to know the fees of the student we can access it by writing s1.n.fees;