Structure type in C

A structured data type is a compound data type which falls under user-defined category and used for grouping simple data types or other compound data types. This contains a sequence of member variable names along with their type/attributes and they are enclosed within curl brackets.

Syntax of defining structure in C
struct < struct name > {
  < type > < member >;
};

Need for Struct data types

There are some situations when we need to group different types of variables in one group. Let's see one situation here- we want to store the name, roll and age of a student.

unsigned int student_roll;
char student_name [MAX_STRING];
unsigned int student_age;
Here we have a logical grouping between there three variables but still these three variables are scattered. We are accessing three different variables for storing attribute values of a single student. Now how to group this inside one logical entity, like three variables for a single student grouped inside one variable. This type of grouping is called structure. One point to note here is that array can group only same type elements and here structure has different types.

Example of Struct data types

Let's define these again with a structure type

struct student_t {
  unsigned int roll;
  char name[MAX_STRING];
  unsigned int age;

};
struct student_t student1;
student1.roll = <roll value>;
strcpy (student1.name, <name>);
student1.age = <age>

Structure size and memory layout

Structure is user defined type to group of different type of variables of either compiler defined legacy types or other user defined types or mixed. Individual entity of a structure element is called member. Members inside a structure are placed sequentially next to next in the memory layout. Thus minimum size of a structure is the sum total of all sizes of members, with considering padding.

structure in C

Struct data types syntax

struct <name> {
  <type> <member name 1>;
  <type> <member name 2>;
  ...
  <type> <member name N>;
};

typedef Struct in C

Struct datatypes are never used directly. A good programming practice is to typedef an meaningful alias. These typedef alias are used in source files. This enhances readability and understandability.

typedef struct _student_t {
  unsigned int roll;
  char name[MAX_STRING];
  unsigned int age;

} student_t, *student_ptr

Nested structure

A nested structure is which contains one or more other structures inside it. Say we have a structure to hold the attributes of a student. Student will have attributes of a person or human being. Now we can define another structure called address. An address structure will have street, city, country and PIN code. Now each student should have an address so we can include a member called address of struct address_t in our student_t. Here student structure becomes a nested structure or composite structure.


typedef struct _address_t {
  char street[50];
  char city[30];
  char country[30];
  int pin;

} address_t, *address_ptr;

typedef struct _student_t {
  unsigned int roll;
  char name[MAX_STRING];
  unsigned int age;
  address_t    address;

} student_t, *student_ptr;

Array of structure

Array of structure is a collection of same type of structure variables. Programmers can take an array of structures to view of modify elements. A loop of for or while or do-while may be needed to iterate each elements.

typedef struct _student_t {
  unsigned int roll;
  char name[MAX_STRING];
  unsigned int age;

} student_t, *student_ptr;

/* Array of structures */
student_t students[100];

/* Iterate an array of structure */
for (= 0; i < 100; i++) {
  students[i] /* Access each element */
}

Another good advantage of using array of structure is saving or retrieving entire block of structures in files is very easy.

Pointer of structure

Pointer of structure is pointer type to point a structure variable. This can be used to point a point a global or local variable. Another use of taking a pointer of structure is to allocate dynamic array of structure.

typedef struct _student_t {
  ..
} student_t, *student_ptr;

/* Pointer of structure */
student_ptr students_p;

/* Dynamic array of structure */
students_p = (student_ptr) malloc(sizeof(student_t)*100);

/* Iterate an array of structure */
for (= 0; i < 100; i++) {
  students_p->name /* Access each element */
}
/*Free pointer of structure */
free(students_p);

Linked structure or linked list

Linked structure is one which contains address or pointer of the adjacent structure variable. Linked structure points to next structure and next structure does the same. So they form a chain of structures or often known as linked list.

typedef struct _node_t {
  int value;
  struct _node_t *next;
} node_t, *node_ptr;

/* Pointer of structure */
node_ptr head_p;

/* Head structure */
head_p = (node_ptr) malloc(sizeof(node_t));
head_p->value = 1;
head_p->next = NULL;

head_p->next = (node_ptr) malloc(sizeof(node_t));
head_p->next->value = 2;
head_p->next->next = NULL;

Structure usage & applications

Structures are the most applicable field in any C program. Practical use of structure data type is count less. However below are some frequent applicable fields-

  1. Student, Employee, Customer etc data records,
  2. Objects displayed in Video games,
  3. Nodes in linked list, queue, LIFO,
  4. Binary tree and other Tree type objects
  5. File, Folders, volumes, partitions in file system and explorer nodes,
  6. All the GUI elements, Window, buttons, menu items,
  7. Header attributes of data files like, images (bitmaps, PNG, JPEG, GIF), sounds(wav, mp3, midi), video(mp4, avi, DIVX),
  8. Data structures used in OS (TASK, IPC, locks, device, driver etc),
  9. Frames, Packets,etc in TCP IP and OSI communication layers
  10. Any application or services handing objects.

Structure demo example

/* Structure type demo example program */
#include<stdio.h>

/* Structure type student */
typedef struct student {
  char name[100];
  char dept[100];
  int rollno;
  float marks;
} student_t, *student_ptr;

/* Structure type main routine */
int main (int argc, char *argv[])
{
  /* declare struct variable */
  student_t s1;
  student_ptr s;
  s = &s1;
  printf("\nEnter the name, dept, roll number and marks of student:\n");
  scanf("%s %s %d %f", s1.name, s1.dept, &s1.rollno, &s1.marks);
  printf("\nThe name, dept, roll number and marks of the student are:");
  printf("\n%s %s %d %.2f",s->name, s->dept, s->rollno, s->marks);

}

Program output

Enter the name, dept, roll number and marks of student:
Student1 ECE 1 96.5

The name, dept, roll number and marks of the student are:
Student1 ECE 1 96.50

About our authors: Team EQA

You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.

#