C Structures
C Structures
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;
....
....
};
C 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:
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
}variable1, varaible2, ...;
Syntax
structure_name.member1;
strcuture_name.member2;
The reason for the above error is simple. When a datatype is declared, no
memory is allocated for it. Memory is allocated only when variables are
created.
Example of Structure in C
The following C program shows how to use structures
// C program to illustrate the use of structures
#include <stdio.h>
// Driver code
int main()
{
// variable declaration after structure template
// initialization with initializer list and designated
// initializer list
struct str1 var1 = { 1, 'A', 1.00, "GeeksforGeeks" },
var2;
struct str2 var3 = { .ff = 5.00, .ii = 5, .cc = 'a' };
return 0;
}
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.
Example
struct parent {
int member1;
struct member_str member2 {
int member_str1;
char member_str2;
...
}
...
}
In this method, two structures are declared separately and then the member
structure is nested inside the parent structure.
Example
struct member_str {
int member_str1;
char member_str2;
...
}
struct parent {
int member1;
struct member_str member2;
...
}
One thing to note here is that the declaration of the structure should always
be present before its definition as a structure member. For example, the
declaration below is invalid as the struct mem is not defined when it is
declared inside the parent structure.
struct parent {
struct mem a;
};
struct mem {
int var;
};
We can access nested Members by using the same ( . ) dot operator two
times as shown:
str_parent.str_child.member;
// driver code
int main()
{
struct parent var1 = { 25, 195, 'A' };
Uses of Structure in C
C structures are used for the following:
1. The structure can be used to define the custom data types that can
lists, etc.
4. 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.
the structure.
body.