C Structures

The structure is a user-defined data type in C, which is used to store a collection of different kinds of data.

  • The structure is something similar to an array; the only difference is array is used to store the same data types.
  • struct keyword is used to declare the structure in C.
  • Variables inside the structure are called members of the structure.

For example: If I have to write a program to store Student information, which will have Student’s name, age, branch, permanent address, father’s name etc, which included string values, integer values etc, how can I use arrays for this problem, I will require something which can hold data of different types together.

In structure, data is stored in form of records.

Arrays and Strings   => Similar data (int, float, char)

Structures can hold  => dissimilar data

Syntax for creating Structures

A C Structure can be created as follows:

struct employee{

int code;                                  => this declares a new user-defined datatype

float salary;

char name[10];

};                                              • semicolon is important

We can use this user-defined data type as follows:

struct employee e1;          // creating a structure variable
strcpy(e1.name,”programiz”);
e1.code=100;
e1.salary=71.22;

So a structure in c is a collection of variables of different types under a single name.

Quick Quiz: Write a program to store the details of 3 employees from user-defined data. Use the structure declared above.

Why use structures?

We can create the data types in the employee structure separately but when the number of properties in a structure increases, it becomes difficult for us to create data variables without structures. In a nutshell:

  1. Structures keep the data organized.
  2. Structures make data management easy for the programmer.
Array of Structures

Just like an array of integers, an array of floats, and an array of characters, we can create an array of structures.

struct employee facebook[100];     =>an array of structures

We can access the data using:

facebook[0].code=100;

facebook[1].code=101;

……….and so on.

Initializing structures

Structures can also be initialized as follows:

struct employee programiz={100,71.22,”programiz”};

struct employee shubh={0};                   // All the elements set to 0
Structures in memory

Structures are stored in contiguous memory locations for the structures e1 of type struct employee, memory layout looks like this:

In an array of structures, these employee instances are stored adjacent to each other.

Pointer to structures

A pointer to the structure can be created as follows:

struct employee *ptr;

ptr=&e1;

Now we can print structure elements using:

printf(“%d”,*(ptr).code);

Arrow operator

Instead of writing *(ptr).code, we can use an arrow operator to access structure properties as follows

*(ptr).code or ptr->code

Here -> is known as an arrow operator.

Passing Structure to a function

A structure can be passed to a function just like any other data type.

void show(struct employee e);   =>Function prototype

Quick Quiz: Complete this show function to display the content of the employee.

Typedef keyword

We can use the typedef keyword to create an alias name for data types in c.

typedef is more commonly used with structures.

struct complex{
float real;	                       // struct complex c1,c2; for defining complex numbers
float img;
};

typedef struct complex{
float real;
float img;	                    // ComplexNo c1,c2; for defining complex numbers
}ComplexNo;

Exercise on Structure

  • Create a two-dimensional vector using structures in C.
  • Write a function SumVector which returns the sum of two vectors passed to it. The vectors must be two-dimensional.
  • Twenty integers are to be stored in memory. What will you prefer- Array or Structure?
  • Write a program to illustrate the use of an arrow operator -> in C.
  • Write a program with a structure representing a Complex number.
  • Create an array of 5 complex numbers created in problem 5 and display them with the help of a display function. The values must be taken as an input from the user.
  • Write problem 5’s structure using typedef keyword.
  • Create a structure representing a bank account of a customer. What fields did you use and why?
  • Write a structure capable of storing date. Write a function to compare those dates.
  • Solve problem 9 for time using typedef keyword.

Was this article helpful?
YesNo

Leave a Comment

Your email address will not be published. Required fields are marked *