Structures in C
Structures in C
Topperworld.in
Structures
The structure in C is a user-defined data type that can be used to group
items of possibly different types into a single type.
The struct keyword is used to define the structure in the C programming
language.
The items in the structure are called its member and they can be of any
valid data type.
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:
Syntax
struct structure_name {
data_type member_name1; data_type member_name1;
....
....
};
Structure Definition
To use structure in our program, we have to define its instance. We can
do that by creating variables of the structure type.
We can define structure variables using two methods:
©Topperworld
C Programming
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
}variable1, varaible2, ...;
Syntax
structure_name.member1;
strcuture_name.member2;
In the case where we have a pointer to the structure, we can also use the
arrow operator to access the members.
©Topperworld
C Programming
Example:
#include <stdio.h>
©Topperworld
C Programming
int main() {
// Declare a variable of type 'struct Person'
struct Person person1;
return 0;
}
Output:
Name: John
Age: 25
// defining structure
struct str1 {
int a;
};
©Topperworld
C Programming
int main()
{
// creating structure variables using new names
str1 var1 = { 20 };
str2 var2 = { 314 };
return 0;
}
Output:
var1.a = 20
var2.x = 314
Nested Structures
C language allows us to insert one structure into another as a member.
This process is called nesting and such structures are called nested
structures.
There are two ways in which we can nest one structure into another:
In this method, the structure being nested is also declared inside the parent
structure.
©Topperworld
C Programming
In this method, two structures are declared separately and then the member
structure is nested inside the parent structure.
Structure Pointer in C
We can define a pointer that points to the structure like any other variable.
Such pointers are generally called Structure Pointers. We can access the
members of the structure pointed by the structure pointer using the ( -> )
arrow operator.
Example:
#include <stdio.h>
struct Student {
int rollNumber;
char name[50];
float marks;
};
int main() {
// Declare a structure variable
struct Student student1;
// Access structure fields using the structure pointer and arrow operator
printf("Roll Number: %d\n", ptr->rollNumber);
printf("Name: %s\n", ptr->name);
printf("Marks: %.2f\n", ptr->marks);
return 0;
©Topperworld
C Programming
Output:
Roll Number: 101
Name: John
Marks: 85.50
Uses of Structure in C
C structures are used for the following:
The structure can be used to define the custom data types that can be
used to create some complex data types such as dates, time, complex
numbers, etc. which are not present in the language.
It can also be used in data organization where a large amount of data can
be stored in different fields.
Structures are used to create data structures such as trees, linked lists,
etc.
They can also be used for returning multiple values from a function.
Limitations of C Structures
In C language, structures provide a method for packing together data of
different types. A Structure is a helpful tool to handle a group of logically
related data items. However, C structures also have some limitations.
Higher Memory Consumption: It is due to structure padding.
No Data Hiding: C Structures do not permit data hiding. Structure
members can be accessed by any function, anywhere in the scope of the
structure.
Functions inside Structure: C structures do not permit functions inside
the structure so we cannot provide the associated functions.
Static Members: C Structure cannot have static members inside its body.
Construction creation in Structure: Structures in C cannot have a
constructor inside Structures.
©Topperworld