Linux C Programming Tutorial Part 23 - Structures

So far in this ongoing C programming tutorial series, we have discussed several aspects, ranging from variables to functions to even pointers. However, that's still like scratching the surface, as there are many other important concepts in the C programming language.

Today, in this tutorial, we will discuss one such concept - the concept of structures.

Structures in C Programming Language

Here's how a structure is declared:

struct TAG {
variable declaration
variable declaration
...
...
...
};

TAG can be any name you want to give to this structure, like emp_details.

It should be common knowledge by now that variables are used to store values in a C program. Up until now, we have seen programs/code using one or more variables, but have never seen multiple variables being grouped together. Yes, that's possible, and that's essentially what structures do.

Imagine a case wherein the code you write deals with the maintenance of employee details at a company. Every time a new employee joins, your program is fed with the employee's name, age, designation, and department. So, in this case, it's ideal to have the code written in a way that all this information related to an employee is treated as a single unit.

So one option to write the code this way is to create a structure that groups all this employee related information. All you need to do is to create an instance of the structure (or a variable of that structure type) and fill up all details related to the new employee.

Following are a couple of ways in which you can create structure type variables (a, b, and c in this case):

struct emp_details {
variable declaration
variable declaration
...
...
...
}a, b, c;
struct emp_details a;
struct emp_details b;
struct emp_details c;

Enough of theory, let's take an easy to understand example. Suppose your team consists of 2 members, and you want to have their details stored in your computer through a program that you want to code in C. The following piece of code should give you a basic idea on how structures can be helpful in that case:

#include <stdio.h>

struct emp_details {

int emp_code;
int emp_age;

};

int main()
{
struct emp_details employee[2];

printf("\n enter code and age of first member\n");
scanf("%d", &employee[0].emp_code);
scanf("%d", &employee[0].emp_age);

printf("\n enter code and age of second member\n");
scanf("%d", &employee[1].emp_code);
scanf("%d", &employee[1].emp_age);

for(int i=0; i<2; i++)
{
printf("\n Code and age of member %d is: %d and %d \n", i+1, employee[i].emp_code, employee[i].emp_age);
}

return 0;
}

So here in this code, we first declared a structure named 'emp_details' with 'emp_code' and 'emp_age' as its two member variables. Then in the 'main' function, we defined two objects of this structure in the form of an array. Thereon, we populated member variables corresponding to these objects by accepting input from the user. And finally, the program outputs these values back to the user.

Here's the output when we executed the program on our machine:

enter code and age of first member 
105
27

enter code and age of second member
110
32

Code and age of member 1 is: 105 and 27

Code and age of member 2 is: 110 and 32

Variables that are part of a structure unit can be of any type. You can even have objects/instance of a different structure as part of your structure. All this and more in our next tutorial on structures, so stay tuned.

Conclusion

This tutorial focused on the basics of structures in C programming language. We learned how to declare a structure, then define an object or instance for it, and finally, how to access structure variables. In our upcoming tutorials, we will delve a bit deeper and learn more about structures.

Share this page:

Suggested articles

2 Comment(s)

Add comment

Comments

By: Garyy Stewart at: 2019-04-24 12:44:10

Using two ints as a description for a structure doesn'r work very well as it might confuse some people about the

difference between a structure and an array.. An int and a float or string would be a much better example. The

whole point of structures is to group different kinds of variables into one data type, the structure.

By: iu1j4 at: 2019-04-25 04:56:57

there is anothere way to declare a struct. We can use typedef to make a new type of data. For example:

typedef struct {

int id;

char *name;

} record_t;

 

record_t *records

 

records = (record_t *)malloc(100*sizeof(record_t));