Structures In C
Hey there! In this article, we will discuss C structures in detail, their uses, advantages, and disadvantages with examples.
-
A structure in C is a derived or user-defined data type. We use the keyword struct to define a custom data type that groups together the elements of different types. The difference between an array and a structure is that an array is a homogenous collection of similar types, whereas a structure can have elements of different types stored adjacently and identified by a name.
-
We are often required to work with values of different data types having certain relationships among them. For example, a book is described by its title (string), author (string), price (double), number of pages (integer), etc. Instead of using four different variables, these values can be stored in a single struct variable.
Video Explanation

1. C Structure Declaration
We have to declare structure in C before using it in our program. In structure declaration, we specify its member variables along with their datatype. We can use the struct keyword to declare the structure in C using the following syntax:
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
};
The above syntax is also called a structure template or structure prototype and no memory is allocated to the structure in the declaration
2. C Structure Definition
A. Structure Variable Declaration with Structure
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
}variable1, varaible2, ...;
Example
struct book{
char title[50];
char author[50];
double price;
int pages;
} book1;
B. Structure Variable Declaration after Structure
// structure declared beforehand
struct structure_name variable1, variable2, .......;
Example
struct book book1;
3. Structure Initialization
A. Initialization using Assignment Operator
struct structure_name str;
str.member1 = value1;
str.member2 = value2;
str.member3 = value3;
.
.
.
B. Default Initialization
By default, structure members are not automatically initialized to 0 or NULL. Uninitialized structure members will contain garbage values. However, when a structure variable is declared with an initializer, all members not explicitly initialized are zero-initialized.
struct Point
{
int x;
int y;
};
struct Point p = {0}; // Both x and y are initialized to 0
C. Initialization using Initializer List
The initialization of a struct variable is done by placing the value of each element inside curly brackets.
Example
struct book book1 = {"Learn C", "Dennis Ritchie", 675.50, 325};
D. Initialization using Designated Initializer List
struct structure_name str = { .member1 = value1, .member2 = value2, .member3 = value3 };
4. Accessing the Structure Members
We can access structure members by using the ( . ) dot operator.
Syntax
structure_name.member1;
structure_name.member2;
Example
#include <stdio.h>
struct book{
char title[10];
char author[20];
double price;
int pages;
};
void main(){
struct book book1 = {"Learn C", "Dennis Ritchie", 675.50, 325};
printf("Title: %s \n", book1.title);
printf("Author: %s \n", book1.author);
printf("Price: %lf\n", book1.price);
printf("Pages: %d \n", book1.pages);
printf("Size of book struct: %d", sizeof(struct book));
}
Output
Title: Learn C
Author: Dennis Ritchie
Price: 675.500000
Pages: 325
Size of book struct: 48
In the case where we have a pointer to the structure, we can also use the arrow operator to access the members.
Syntax
struct_pointer->title;
Example
#include <stdio.h>
#include <string.h>
struct book{
char title[10];
char author[20];
double price;
int pages;
};
int main (){
struct book book1 = {"Learn C", "Dennis Ritchie", 675.50, 325};
struct book *strptr;
strptr = &book1;
printf("Title: %s \n", strptr -> title);
printf("Author: %s \n", strptr -> author);
printf("Price: %lf \n", strptr -> price);
printf("Pages: %d \n", strptr -> pages);
return 0;
}
Output
Title: Learn C
Author: Dennis Ritchie
Price: 675.500000
Pages: 325
Note :
The dot (.) operator is used to access the struct elements via the struct variable. To access the elements via its pointer, we must use the indirection (->) operator
5. typedef for Structures
The typedef keyword is used to define an alias for the already existing datatype. In structures, we have to use the struct keyword along with the structure name to define the variables. Sometimes, this increases the length and complexity of the code. We can use the typedef to define some new shorter name for the structure.
Example
#include <stdio.h>
// defining structure
typedef struct {
int a;
} str1;
// another way of using typedef with structures
typedef struct {
int x;
} str2;
void main()
{
// creating structure variables using new names
str1 var1 = { 20 };
str2 var2 = { 314 };
printf("var1.a = %d\n", var1.a);
printf("var2.x = %d\n", var2.x);
}
Output
var1.a = 20
var2.x = 314