Structures in C
Learn via video course

Overview
Structure in C is a user-defined data type. It is used to bind two or more similar or different data types or data structures together into a single type. The structure is created using the struct keyword, and a structure variable is created using the struct keyword and the structure tag name. A data type created using structure in C can be treated as other primitive data types of C to define a pointer for structure, pass structure as a function argument or a function can have structure as a return type.
Scope of Article
- This article gives a basic idea about structure, how to create structure and variables for structure.
- We will also see passing a structure element as a function argument, a pointer to a structure as well as an array of structures.
- This article also discussed how to initialize the structure variables and how to align them.
- Also, we will see the limitations of the structure.
Introduction
In C language, to store the integers, characters, and decimal values, we have int, char, float, or double data types already defined(also known as the primitive data types). Also, we have some derived data types such as arrays and strings, to store similar types of data types elements together. Still, the problem with arrays or strings is that they can only store variables of similar data types, and the string can store only characters. What if we need to store two different data types together in C for many objects? Like, there is a student variable that may have its name, class, section, etc. So if we want to store all of its information, We can create different variables for every variable like a character array to store the name, an integer variable to store the class, and a character variable to store the section. But this solution is a little messy, C provides us with a better neat and clean solution, i.e., Structure.
Why Use Structure?
Imagine we have to store some properties related to a Student like a Name, Class, and Section. We have one method to create a character array to store the Name, integer variable for Class, and character variable for Section, like:
Syntax:
Storing data for a single student is easy, but imagine creating that many variables for 50 students or even 500 or more. So to handle this type of problem, we need to create a user-defined data type that can store or bind different types of data types together, this can be done with the help of structure in C.
What Is a Structure?
The structure is a user-defined data structure that is used to bind two or more data types or data structures together. For storing the details of a student, we can create a structure for a student that has the following data types: a character array for storing name, an integer for storing roll number, and a character for storing section, etc. Structures don't take up any space in the memory unless and until we define some variables for it. When we define its variables, they take up some memory space which depends upon the type of the data member and alignment (discussed below).
How to Create a Structure?
To create a structure in C, the struct keyword is used followed by the tag name of the structure. Then the body of the structure is defined, in which the required data members (primitive or user-defined data types) are added.
Syntax:
In the above syntax, the data_members can be of any data type like int, char, double, array or even any other user-defined data type. The data_member_definition for the data types like character array, int, and double is just a variable name like name, class, and roll_no. We have also declared a variable, i.e., student1 of the Student structure. Please note that it is not mandatory to always declare structure variables in this way. We will see other ways in the coming sections.
How to Declare Structure Variables?
If we have created a Student structure for storing data of students with all the data members like student name, student class and student section, how can we use them? To use the properties of the created structure in C, we have to create structure variables. There are two ways to declare variables for structure in C language:
- First Way:
Syntax:
In the above example, Student structure is created and the student1 variable is declared for it just after the structure definition.
- Second Way:
As we create a structure in C, we have created a user-defined data type. So this data type can be treated as the primitive data type while declaring a variable for that structure.
Syntax:
Which Approach of Declaring Structure Variables Is Better?
If we declare the structure variables with the structure definition, they work as global variables(means they can be accessed in the whole program). If we need global variables, we can declare variables with the structure otherwise declaring it using the second approach is the best way as it is easy to maintain or initialize variables.
How to Initialize Structure Members?
Initializing a structure member means assigning values to the structure members according to their respective data types. But declaration doesn't allocate memory for the structure. When we declare a variable for a structure, only then is the memory allocated to that structure variable. Hence, assigning value to something that doesn't have memory is the same as serving food without a plate, which isn't a good idea! In short, structure members cannot be initialized during the declaration. For example:
Syntax:
This initialization of structure will give an error. So, how can we initialize the members then? Actually, there are three ways to initialize structure members:
- Using dot '.' operator
- Using curly braces ‘{}’
- Designated initializers
- Using Dot '.' operator
Using the dot (.) operator, we can access any structure member and then initialize or assign its value according to its data type.
Syntax:
In the above syntax first, we created a structure variable, then with the help of the dot operator accessed its member to initialize them.
Let’s take an example to understand the above syntax:
- Example
- Output
In the above code, we have created a structure, Student and declared some members in it. After that, we created an instance (variable or object of structure Student) for it to access the structure members using the dot operator and assigned value to them. Also, we used strcpy method of the string, this is used to assign the value of one string to another. In the end, we output the values of structure members with the help of the dot operator.
- Using curly braces ‘{}’
If we want to initialize all the members during the structure variable declaration, we can declare using curly braces.
Syntax:
To initialize the data members by this method, the comma-separated values should be provided in the same order as the members declared in the structure. Also, this method is beneficial to use when we have to initialize all the data members.
- Example
- Output
In the above code, first we created a structure, Student. After that, we create a variable for the structure and initialized its members using curly braces in the same order as the data members are declared inside the structure. At the end print the assigned values.
- Designated initializers
Designated initialization is simple initialization of the structure members and is normally used when we want to initialize only a few structure members, not all of them. We will discuss it in much more detail in the later section of this article.
Structures as Function Arguments
So far, we have learned the declaration, initialization, and printing of the data members of structures. Now, you must wonder how we can pass an entire structure or its members to a function. So, yes, we can do that. While passing structure as a function argument, structure variables are treated the same as variables of primitive data types. The basic syntax for passing structure as a function argument is
Syntax:
Let's see an example for more understanding:
Example
Output:
In the above code, we have created a structure Student and declared some members for it to store the data of students in it. After that, we created an instance and initialized all of the structure members. There were two functions: in function printStudent(), we passed the structure by using the pass-by-value concept, while in function changeStudent(), we passed the structure by pass-by-reference.
While we are passing values by reference we get a structure pointer in the function (We will discuss structure pointers later in this article).
Bit Fields
In C programming, memory is allocated in bits to store every data type. For example, for integer variables, 32 bits are assigned. Bit fields are the concept of Structure in C in which we can define how many bits we have to allocate to the particular data member of Structure to save memory. We can define the number of bits for a particular member using the colon (:) operator.
Syntax:
From the above syntax, we can see that we can change the number of bits for data members as per our requirement by using the colon operator. Let's see an example for a better understanding:
In the above code, we defined two structures for storing the dates.
- The first structure has a size of 12 bytes. It is because there are three integer variables. Each integer variable takes 4 bytes of memory, giving the total size 3 * 4 = 12.
- The second structure has a size of 8 bytes. This is because, in the second structure, we defined the maximum number of bits required to represent the day and month.
Since we know that the day can have a maximum value of 31, it can be easily represented by 5 bits (2 raised to the power of 5 give us 32, so we can store any number up to 31 in it ). Similarly, a month has a maximum value of 12. So, it will require a maximum of 4 bits for its representation (2 raised to the power of 4 is 16, which is greater than 12). The day and month variables both have combined 9 bits, and as they both are integers, so combined 32 bits (4 bytes) of memory will be allocated for them. Another 4 bytes of memory is required for the year variable. Hence, the total size is 4 + 4 = 8 bytes.
We can observe that both structures have the same number of data members, but the second one takes less space. So by defining the maximum number of bits, we can save memory.
Accessing Structure Elements
We can directly access the structure member using the dot(.) operator. The dot operator is used between the structure variable name and the structure member name we want to access. Let’s see the syntax to understand it in a better way.
Syntax:
- Example 1
Output:
Here we created a simple structure, Complex to define complex numbers. We created a structure variable var and accessed its structure members: real and imaginary with the help of dot operator and assigned them some value. After that, we printed the values using the dot operator again.
What Is Designated Initialization?
Designated initialization is simple initialization of the structure members and is normally used when we want to initialize only a few structure members, not all of them.
Syntax:
From syntax, we can see that we use curly braces, and in between them, with the help of the dot operator, data members are accessed and initialized. There can be any number of structure members from a single structure that we can initialize, and all of them are separated using commas. But the most important thing is that we can initialize members in any order. It is not compulsory to maintain the same order as the members are declared in the structure.
- Example
In the above example, we can see that we have initialized only two members of the structure. Also, note that they are not initialized in the order as they were declared in the structure.
- Output
What Is an Array of Structures?
When we create an array of any primitive data type with size five, do you know what happens? An array consisting of 5 memory blocks is created, and each block works the same way as a single variable of the same data type. As a structure in C is a user-defined data type, we can also create an array of it, same as other data types.
Syntax:
From the above syntax, we created an array of structures with each memory block storing a single structure variable.
- Example
Input:
- Output:
In the above code, we have created a structure and then an array of size 5 to store five structure elements. After that, we accessed structure members using array index to take input or assign values. We can pass an array of structures as a function argument as well.
For example:
Input:
Output:
In the above code, we have created a structure, Student, and then an array, arr of size 5 to store five structure elements. After that, we accessed structure members using array index to take input or assign values. We created a function, print(), that takes two parameters: an array of structure and size of the array. In this function, we printed all the values of each array block.
Nested Structures
The nested word means placed or stored one inside the other. As the structure in C is a user-defined data type so while creating a structure, we can define another structure as its data member, which leads to a structure with another structure inside it. Even the nested structure can have its nested structure.
Syntax 1:
In the above syntax, structure_1 is defined first then nested into another one, i.e., structure_2.
Syntax 2:
In the above syntax, we defined the structure_1 inside the structure_2. As we create a structure inside another one, we can define variables for this structure as we normally define for the structures.
To initialize the structure variables either we can access every data member using a simple dot operator or if we are going to initialize using curly braces, then we have to maintain the same order of data members as they were defined in the structure so for nested structure members also maintain the order, as we initialized variable v1 in above example.
- Example
Input:
Output:
In the above example, we have created a structure, Student with a nested structure, Address inside it. We initialized the data members of the Student structure variable at the end of the structure. Two more structures are created: Subject and Teacher. Subject structure is nested in Teacher Structure. In the main function, we created a variable for Teacher, took user input for all its members, and then printed them using the printf() statements. There can be two ways to create nested structures. The first way is to create a structure (like Subject) and add it to another structure (like Teacher) as a data member or define the structure (like Address) inside another structure (like Student).
Use of typedef in Structure
The typedef is a keyword in the C language used to give an alias to a data type, any syntax, or a part of code. The primary purpose of typedef is to make code short, increasing the code's readability. To declare a structure variable, we write struct keyword first, then structure name, and then the variable name, which is a little long. To give a short name to the structure, we can use typedef. Let’s see the syntax to understand precisely how it works:
Syntax:
We have defined two ways to use typedef with structure in the above syntax. In the first case, we have typedef the structure after declaring it, while in the second, the structure has been typedef during declaration. Also, new_name could be the same as structure_name. For example:
- Output:
We have created a structure, Complex in the above code and declared it with typedef using both of the syntaxes discussed. So, essentially, we have two aliases for struct Complex, i.e., Complex and c. In the main function, we declared three structure variables for the same structure using three different ways. First, we declared in a general way. Second, we declare using Complex alias and lastly using c alias. At last, we assigned some values to this and then printed them all.
What Is a Structure Pointer?
A pointer is a variable that stores the address of another variable. As a structure consists of some data types or data structures for which memory is allocated to the structure variable, we can use a structure pointer to store the address of that memory. A structure pointer is essentially a pointer to a structure variable. Please note that we use the arrow operator (->) to access the structure member using a pointer.
Syntax:
In the syntax above, we first declared a structure variable and then a structure pointer pointing to that variable.
Output:
In the above code, we first created a structure, Complex, to store the real and imaginary values of a complex number. We also declared a structure variable, c, and initialized its data members. Then in the main() function, we created a pointer, cptr of Complex type, and assigned it the address of structure variable, c. Thereafter, we accessed structure members using the arrow operator and assigned them values. In the end, we printed the values of data members using the arrow operator.
What Is Structure Member Alignment?
As soon as a structure variable is declared, we know that memory is allocated to it according to the variable's data type. A structure consists of different data members, so if they are not aligned properly, there will be memory wastage. To reduce the memory waste by the random declaration of data members, we give them proper alignment (i.e., proper order) by defining them in decreasing order of their memory size.
Output:
We have declared two structures in the above code, and both have the same data members. The only difference is in their order of declaration. The first structure has the size of 32 bytes, while the second has 24 bytes due to alignment only. So, to reduce memory loss, while declaring a structure, always declare the data members in decreasing order of memory size requirement.
C Structures Limitations
Structures in C have many limitations as compared to other user-defined data types in other languages. Structures in C does not provide the data hiding property(by which we can make some members private and they cannot be accessed from outside of structure) and every member of the structure can be accessed. We cannot define functions inside the Structures in C, so there is no constructor and as structures do not have their own memory so we cannot ever initialize our data members inside it. If the alignment of the Structure members is not correct then they may cause some loss of memory.
Conclusion
- Structure in C is a user-defined data type. It binds the two or more data types or data structures together.
- The Structure is created using struct keyword and its variables are created using struct keyword and structure name.
- A data type created using structure in C can be treated as other primitive data types of C to declare a pointer for it, pass it as a function argument, or return from the function.
- There are three ways to initialize the structure variables: using the dot operator, using curly braces, or designated initialization.
- One structure can consist of another structure, or more means there can be nested Structures.
- With the help of typedef, we can give short or new names to a structure data type.