C Programming Structure

In this article, you'll learn about C programming structures; what they are, and how to define and use structures in your program.

Structure is a collection of variables (can be of different types) under a single name.

For example: You want to store information about a person: his/her name, citizenship number and salary. You can create different variables name, citNo and salary to store these information separately.

What if you need to store information of more than one person? Now, you need to create different variables for each information per person: name1, citNo1, salary1, name2, citNo2, salary2 etc.

A better approach would be to have a collection of all related information under a single name Person structure, and use it for every person.


How to define a structure?

Keyword struct is used for creating a structure.


Syntax of structure

struct structure_name 
{
    data_type member1;
    data_type member2;
    .
    .
    data_type memeber;
};

Here is an example:

struct Person
{
    char name[50];
    int citNo;
    float salary;
};

This declaration above creates the derived data type struct Person.


Create structure variable

When a structure is defined, it creates a user-defined type. However, no storage or memory is allocated.

Here' how we structure variables:

struct Person
{
    char name[50];
    int citNo;
    float salary;
};

int main()
{
    struct Person person1, person2, p[20];
    return 0;
}

Another way of creating a structure variable is:

struct Person
{
    char name[50];
    int citNo;
    float salary;
} person1, person2, p[20];

In both cases, two variables person1, person2, and an array variable p having 20 elements of type struct Person are created.


How to Access members of a structure?

There are two types of operators used for accessing members of a structure.

  1. Member operator(.)
  2. Structure pointer operator(->) (will be discussed in structure and pointers)

Suppose, you want to access salary of person2. Here's how you can do it:

person2.salary

Example: C structure

// Program to add two distances which is in feet and inches
#include <stdio.h>
struct Distance
{
    int feet;
    float inch;
} dist1, dist2, sum;

int main()
{
    printf("1st distance\n");
    printf("Enter feet: ");
    scanf("%d", &dist1.feet);

    printf("Enter inch: ");
    scanf("%f", &dist1.inch);
    printf("2nd distance\n");

    printf("Enter feet: ");
    scanf("%d", &dist2.feet);

    printf("Enter inch: ");
    scanf("%f", &dist2.inch);

    // adding feet
    sum.feet = dist1.feet + dist2.feet;
    // adding inches
    sum.inch = dist1.inch + dist2.inch;

    // changing feet if inch is greater than 12
    while (sum.inch >= 12) 
    {
        ++sum.feet;
        sum.inch = sum.inch - 12;
    }

    printf("Sum of distances = %d\'-%.1f\"", sum.feet, sum.inch);
    return 0;
}

Output

1st distance
Enter feet: 12
Enter inch: 7.9
2nd distance
Enter feet: 2
Enter inch: 9.8
Sum of distances = 15'-5.7"

Keyword typedef

Keyword typedef can be used to simplify syntax of a structure.

This code

struct Distance{
    int feet;
    float inch;
};

int main() {
    structure Distance d1, d2;
}

is equivalent to

typedef struct Distance{
    int feet;
    float inch;
} distances;

int main() {
    distances dist1, dist2, sum;
}

Nested Structures

You can create structures within a structure in C programming. For example:

struct complex
{
 int imag;
 float real;
};

struct number
{
   struct complex comp;
   int integers;
} num1, num2;

Suppose, you want to set imag of num2 variable to 11. Here's how you can do it:

num2.comp.imag = 11;

More on Structures