Structure in C

If you are into programming, be it a newbie or an expert you might be well-versed with the primitive data types. Primitive data types are used very often, but they can handle values of only one type. 

Now think, if you have to handle heterogeneous data then what will you do? You may go the brute-force way, sometimes it may work at the cost of decreasing the efficiency of your program but when the time arrives for robust problems primitive data types may fail. This is where the need for Structures comes.

What is a structure?

A structure is a user-defined data type that is created by the user according to his/her convenience. 

How does a structure help?

A structure helps in handling a group of items involving various types of primitive data types for a single entity.

Syntax:

The creation of a structure is done with the help of the keyword struct followed by the name of the structure.

Inside it, the variables are declared that will be used.

Code:

C Programs

struct marks
{
    int roll;
    float average;
};

Declaration of Structure 

There are two ways to declare a structure.

  • The first way is to create the structure and then declare using a variable in the main block.

Code:

C Program

//(Basic Way)
struct marks {
  int roll;
  float average;
};

int main() {
  struct marks m1;
}
  • Using a variable for the entire structure

Code:

C Programs

//(Using a variable for the entire structure)
struct marks {
  int roll;
  float average;
}m1;

Accessing the Structure Elements

  • Using the basic way

Code

C Program

// Accessing structure elements using basic declaration
#include<stdio.h>

struct marks {
  int roll;
  float average;
};

int main() {
  struct marks m1;
  m1.roll = 1;
  m1.average = 90.1;
  printf("The roll number is = %d\n", m1.roll);
  printf("The average is = %0.1f", m1.average);
  return 0;
}

Output:

The roll number is = 1
The average is = 90.1

  • Using a separate variable for the entire structure.

Note:- This kind of declaration is very useful when there are multiple or nested structures.

Code

C Program

//Accessing the structure elements using a variable for the entire structure
#include<stdio.h>
struct marks {
  int roll;
  float average;
}m1;

int main() {

  m1.roll = 1;
  m1.average = 90.1;
  printf("The roll number is = %d\n", m1.roll);
  printf("The average is = %0.1f", m1.average);
  return 0;
}

Output:

The roll number is = 1
The average is = 90.1

Array of Structure/Structures

An array of Structures is used to store multiple values of the defined structure type.

Code:

C Program

#include<stdio.h>

struct marks {
  int roll;
  float average;
};

int main() {
  struct marks m1[5];

  printf("Enter the roll number and marks of 5 students. The marks of each 
  student should be rounded up to one decimal place.\n");
  for (int i = 0; i < 5; i++) {
    scanf("%d", & m1[i].roll);
    scanf("%f", & m1[i].average);
  }
  printf("The respective roll number and marks are:-\n");
  for (int i = 0; i < 5; i++) {
    printf("\n");
    printf("Roll number is %d\n ", m1[i].roll);
    printf("The marks obtained is %0.1f\n", m1[i].average);
  }
  return 0;
}

Input:

Enter the roll number and marks of 5 students.
The marks of each student should be rounded
up to one decimal place.
1
91.0
2
92.0
3
98.0
4
90.0
5
92.0

Output:

Enter the roll number and marks of 5 students.
The marks of each student should be rounded
up to one decimal place.

The respective roll number and marks are:-

Roll number is 1
The marks obtained is 91.0

Roll number is 2
The marks obtained is 92.0

Roll number is 3
The marks obtained is 98.0

Roll number is 4
The marks obtained is 90.0

Roll number is 5
The marks obtained is 92.0

Structure Pointer

Pointers store the address/location of another variable.

Structures also have pointers. The members are accessed using an arrow(->).

Code:

C Program

#include<stdio.h>

struct marks {
  int roll;
  float average;
};

int main() {
  struct marks m1;
  m1.roll = 1;
  m1.average = 90.1;

  struct marks * m2 = & m1;
  printf("The roll number is = %d\n", m2 -> roll);
  printf("The average is = %0.1f", m2 -> average);
  return 0;
}

Output:

The roll number is = 1
The average is = 90.1

Multiple Structures in C:

A program can have more than one structure as per the requirement.

Code:

C Program

#include<stdio.h>

struct student {
  int roll;
}
s1;
struct marks {
  float average;
}
m1;
int main() {
  s1.roll = 1;
  m1.average = 91;
  printf("The roll number is = %d\n", s1.roll);
  printf("The average is = %0.1f", m1.average);
  return 0;
}

Output:

The roll number is = 1
The average is = 91.0

Nested Structure in C

Sometimes a single structure is not enough. The structure may require another structure for functioning. This is known as nested structure.

Code:

C Program

#include<stdio.h>

struct student {
  int roll;

  struct marks {
    float average;
  }m1;
}s1;

int main() {
  s1.roll = 1;
  s1.m1.average = 91;
  printf("The roll number is = %d\n", s1.roll);
  printf("The average is = %0.1f", s1.m1.average);
  return 0;
}

Output:

The roll number is = 1
The average is = 91.0

Limitations of Structures in C

  1. Mathematical operations cannot be applied.
  2. No access modifiers.
  3. Functions cannot be used in structures
  4. Static members are not permitted inside structures.
  5. Memory wastage can take place.

Special thanks to Ushmita Dutta for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article