C Structures
C 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. Additionally, the values of a
structure are stored in contiguous memory locations.
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;
....
....
};
The above syntax is also called a structure template or structure prototype and no
memory is allocated to the structure in the declaration.
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:
Syntax
structure_name.member1;
strcuture_name.member2;
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
We can initialize structure members in 3 ways which are as follows:
1. Using Assignment Operator.
2. Using Initializer List.
3. Using Designated Initializer List.
1. Initialization using Assignment Operator
struct structure_name str;
str.member1 = value1;
str.member2 = value2;
str.member3 = value3;
.
.
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>
// declaring structure with name str1
struct str1 {
int i;
char c;
float f;
char s[30];
};
// declaring structure with name str2
struct str2 {
int ii;
char cc;
float ff;
} var; // variable declaration with structure template
// Driver code
int main()
{
// variable declaration after structure template, initialization with initializer list
and designated, initializer list
struct str1 var1 = { 1, 'A', 1.00, "c program" },
var2;
struct str2 var3 = { .ff = 5.00, .ii = 5, .cc = 'a' };
// copying structure using assignment operator
var2 = var1;
printf("Struct 1:\n\ti = %d, c = %c, f = %f, s = %s\n",
var1.i, var1.c, var1.f, var1.s);
printf("Struct 2:\n\ti = %d, c = %c, f = %f, s = %s\n",
var2.i, var2.c, var2.f, var2.s);
printf("Struct 3\n\ti = %d, c = %c, f = %f\n", var3.ii,
var3.cc, var3.ff);
return 0;
}
Output
Struct 1:
i = 1, c = A, f = 1.000000, s = c program
Struct 2:
i = 1, c = A, f = 1.000000, s =c program
Struct 3
i = 5, c = a, f = 5.000000
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 be used
to create some complex data types such as dates, time, complex numbers,
etc. which are not present in the language.
2. It can also be used in data organization where a large amount of data can be
stored in different fields.
3. Structures are used to create data structures such as trees, linked lists, etc.
4. They can also be used for returning multiple values from a function.
Limitatins 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.
Array of Structures in C
When dealing with a large set of related data and different data types, organizing
and managing it efficiently is crucial. In C programming, the combination of arrays
and structures i.e. array of structures provides a powerful tool for managing that.
What is Array?
The array is a homogeneous collection of elements stored in the continuous
memory location. The size of the array is fixed and we can randomly access the
elements using their index.
Declaration of Array
array_type array_name[size];
What is Structure?
The structure is one of the user-defined data types in C that can contain elements
of different types as its members.
Declaration of a Structure in C
struct structure_name{
memberType memberName;
...
...
};
Array of Structures
An array whose elements are of type structure is called array of structure. It is
generally useful when we need multiple structure variables in our program.
Output
Emplyee ID: 0 - Employee Name: Amit
Attendence
0123456
Emplyee ID: 1 - Employee Name: Amit
Attendence
0123456
Emplyee ID: 2 - Employee Name: Amit
Attendence
0123456
Emplyee ID: 3 - Employee Name: Amit
Attendence
0123456
Emplyee ID: 4 - Employee Name: Amit
Attendence
0123456
C Unions
The Union is a user-defined data type in C language that can contain elements of
the different data types just like structure. But unlike structures, all the members in
the C union are stored in the same memory location. Due to this, only one member
can store data at the given instance.
C Union Declaration
In this part, we only declare the template of the union, i.e., we only declare the
members’ names and data types along with the name of the union. No memory is
allocated to the union in the declaration.
union union_name {
datatype member1;
datatype member2;
...
};
Keep in mind that we have to always end the union declaration with a semi-colon.
Initialization of Union in C
The initialization of a union is the initialization of its members by simply assigning
the value to it.
var1.member1 = some_value;
One important thing to note here is that only one member can contain some
value at a given instance of time.
Example of Union
// C Program to demonstrate how to use union
#include <stdio.h>
// union template or declaration
union un {
int member1;
char member2;
float member3;
};
// driver code
int main()
{
// defining a union variable
union un var1;
// initializing the union member
var1.member1 = 15;
return 0;
}
Output
The value stored in member1 = 15
Size of Union
The size of the union will always be equal to the size of the largest member of the
array. All the less-sized elements can store the data in the same space without any
overflow.
Structure Union
The size of the structure is equal to or greater The size of the union is the
than the total size of all of its members. size of its largest member.
The structure can contain data in multiple Only one member can contain
members at the same time. data at the same time.