Structures in C

Structure in c language is a user defined datatype that allows you to hold different type of elements.

Difference between arrays and structures

An array is a collection of related data elements of same type. Structure can have elements of different types.

Syntax for defining structure :

//The struct keyword is used to define structure
struct structure_name
{
    data_type member1;
    data_type member2;
    .
    .
    data_type memeberN;
};

Important Points :

  • The closing brace ‘}‘ in the structure type declaration must be followed by a semicolon ‘;‘.
  • Usually structure type declaration appears at the top of the source code file, before any variables or functions are defined.

If you want to store data about a book. Like Data related to name(string)price(float)pages(integer).
Then the structure is defined as:

Example :

struct book
{
    char name[50];
    float price;
    int pages;
};

Explanation :

struct– is a keyword.

book– is the tag name of the structure

namepricepages– are the members or fields of the structure

Declaring a structure variable :

Once the new structure data type has been defined one or more variables can be declared to be of that type.
For example the variables b1b2b3 can be declared to be of the type struct book.

Syntax :

struct book b1, b2, b3;

This statement will create space in the memory to hold all the elements in the structure,
in this case it will be 7 bytes –

  • 1byte for char(name)
  • 4byte for float(price)
  • 2byte for integer(pages)

Examples for declaring structure :

struct book
{
    char name;
    float price;
    int pages;
};

struct book b1, b2, b3;

struct book
{
    char name;
    float price;
    int pages;
} b1, b2, b3;

struct
{
    char name;
    float price;
    int pages;
} b1, b2, b3;

Initializing the structure variables :

    struct book
    {
    char name[10];
    float price;
    int pages;
    };
    struct book b1 = { "abc",50.00, 100 };
    struct book b2 = { "xyz", 200.00, 500 };

‘.’ => member or dot operator to access the pages of the structure book :

    b1.pages

Similarly to refer to price

    b1.price

Example :

    #include<stdio.h>
    #include<conio.h>
    void main( )
    {
    struct book
    {
    char name;
    float price;
    int pages;
    };
    struct book b1 = { 'B', 130.00, 550 };
    printf ("\nName of the book = %c", b1.name);
    printf ("\nPrice of book = %f", b1.price);
    printf ("\nNumber of pages in the book = %d", b1.pages);
    printf ("\nAddress of name = %u", &b1.name);
    printf ("\nAddress of price = %u", &b1.price);
    printf ("\nAddress of pages = %u", &b1.pages);
    }

Output :

Name of the book = B
Price of book = 130.00
Number of pages in the book = 550
Address of name = 65518
Address of price = 65519
Address of pages = 65523

Accessing Structure elements

Example of User Input :

    #include <stdio.h>
    int main(void)
    {
    struct book
    {
    char name[10];
    int price;
    }b1[5];

    int i;
    for(i=0;i<2;i++)
    {
    Printf(“\nEnter Book name : ”);
    scanf("%s",b1[i].name);

    printf(“\nEnter Price :”);
    scanf("%d",&b1[i].price);
    }
    for(i=0;i<2;i++)
    {
    printf("\nName is : %s",b1[i].name);
    printf("\nPrice : %d ",b1[i].price);
    }

    return 0;
    }

Output :

Enter Book name :
Abc
Enter Price :
100
Enter Book Name :
Pqr
Enter price
50
Name is : Abc
Price : 100
Name is : Pqr
Price : 50

Explanation :

Here b1[5] means b1 is structure variable and size is defined as 5 means we can store 5 book details in the memory. So here we are storing 2 book details with its name and price.
For loop is used to get the 2 book details.

Nested structure

Nested structure in c language can have another structure as a member.

There are two ways to define nested structure in c language :

  • By separate structure
  • By Embedded structure

Separate Structures :

We can create 2 structures, but dependent structure should be used inside the main structure as a member.

struct Date
{
    int dd;
    int mm;
    int yyyy;
};
struct Employee
{
    int id;
    char name[20];
    struct Date doj;
}emp1;

As you can see, doj (date of joining) is the variable of type Date. Here doj is used as a member in Employee structure.

Embedded Structure

We can define structure within the structure also.

    struct Employee
    {
    int id;
    char name[20];
    struct Date
    {
    int dd;
    int mm;
    int yyyy;
    }doj;
    }emp1;

Accessing Nested Structure

We can access the member of nested structure by Outer_Structure.Nested_Structure.member as given below :

    emp1.doj.dd
    emp1.doj.mm
    emp1.doj.yyyy

Example of nested structures

    #include <stdio.h>
    #include <string.h>
    struct Employee
    {
    int id;
    char name[20];

    struct Date
    {
    int dd;
    int mm;
    int yyyy;
    }doj;
    }e1;
    int main( )
    {
    //storing employee information
    e1.id=1;
    //copying string into char array
    strcpy(e1.name, "xyz");

    e1.doj.dd=26;
    e1.doj.mm=12;
    e1.doj.yyyy=2014;

    //printing employee information
    printf( "employee id : %d\n", e1.id);
    printf( "employee name : %s\n", e1.name);
    printf( "employee date of joining (dd/mm/yyyy) : %d/%d/%d\n", e1.doj.dd,e1.doj.mm,e1.doj.yyyy);
    return 0;
    }

Output :

employee id : 1
employee name : xyz
employee date of joining (dd/mm/yyyy) : 26/12/2014