A Structure is a collection of dissimilar data type of particular entity.
For example, An entity Student may have its name (string), roll number (int), marks (float). To store such type of information structure can be used.
The struct keyword is used to define the structure in C programming language.
Syntax: struct structure_name{
datatype member1;
datatype member2;
........
........
};
One can declare each of the member of structure inside curly braces.
Syntax: struct mystructure{
int myname;
char myletter;
};
To use structure in our program, we have to define its instance. We can do that by creating variables of the structures type
We can define using 2 methods:
1. Structure variable declaration with structure template
struct structure_name{
datatype member1;
datatype member2;
........
........
}variable1,variable2;
2. Structure variable declaration after structure template
(structure declared beforehand)
struct structure_name variable1,variable2,....;
We can access structure members using (.) dot operator.
Syntax:
structure_name.member1;
structure_name.member2;
Structure member cannot be initialized with declaration.
The reason is when memory is allocated to datatype declared. Memory allocated only when variable is created.
There are 2 ways to initialize structure:
1) . (dot operator) is used to access the structure members using structure variable.
2) -> (arrow operator) is used to access structure members using structure pointer variable.
Array within structure
An
array can be declared inside a structure as a member when we need to store multiple
members of the
same type.
Syntax: struct strucure_name{
Datatype array_Name[arraysize];
.............
};
Example:
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.
Example:
Anonymous Structures
Structures without a
name, usually used for a single occurrence.
Nested Structures
Structures
inside structures.
example:
Pointers in
Structures
The way we can have a pointer pointing to an int, or a pointer pointing to a char, similarly we can have pointer pointing to a struct, such pointers are known as structure pointer.
A structure pointer is
defined as the pointer which
points to the address of the memory block that stores a structure known as the structure pointer. Complex
data structures like linked lists, trees, graphs, etc. are created with the
help of structure pointers. The structure pointer tells the address of a structure
in memory by pointing its variable to the structure variable.
Self-referential
Structures
A
structure containing a member that is a pointer to its own type is self referential structure. A self-referential structure is a structure that contains a member variable that
is a pointer to a variable of the same structure type. This allows the
structure to create linked data structures where elements reference each other .Those
structure in which one or more pointer points to the structure of the same
type.
Pointers in
Structures
The way we can have a pointer pointing to an int, or a pointer pointing to a char, similarly we can have pointer pointing to a struct, such pointers are known as structure pointer.
A structure pointer is
defined as the pointer which
points to the address of the memory block that stores a structure known as the structure pointer. Complex
data structures like linked lists, trees, graphs, etc. are created with the
help of structure pointers. The structure pointer tells the address of a structure
in memory by pointing its variable to the structure variable.
Structure Padding in C
Structure padding is a concept in C
that adds the one or more empty bytes between the memory addresses to align the
data in memory.
Union in C
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.
Bit Fields in C
In C, bit fields are used to specify the size of individual members within a structure. They
allow for more efficient use of memory by packing multiple bit fields into a single storage unit, such
as a byte or a word. This is particularly useful when dealing with hardware registers, protocol
headers, or other situations where memory efficiency is crucial.
typedef in C
The typedef is a
keyword used in C programming to provide some meaningful names to the already
existing variable in the C program. It behaves similarly as we define the alias for the
commands. In short, we can say that this keyword is used to redefine the name
of an already existing variable.
Syntax:
- typedef <existing_name> <alias_name>
- typedef unsigned int unit;
Questions:
Can you access a structure
member without -> operator using pointer to structure?
Yes, you can access
a structure member
without using the ‘->‘ operator
by dereferencing the pointer
using the ‘*‘ operator combined with the dot ‘.‘ operator. For example,
‘(*ptr).member‘.
What are Structures?
A Structure is a collection of dissimilar data type of particular entity.
What is Structure padding?
Structure padding
is the addition of extra
memory to align
data in a structure to word boundaries, which can improve the performance of memory access but may also
increase the size of the structure.
What is meant by an
anonymous structure?
An anonymous structure is a structure definition without a tag or name, often
used for nested
structures to define complex
data models without having to name intermediate structures.
What is are self
referential structures?
Self-referential structures are structures that contain at least one member that is a pointer to the structure of its own type. They are
commonly used for creating linked lists and trees.
What is the use of .(dot)
operator?
The dot ‘.‘ operator is
used to access members of a structure when you have a structure variable.
What is the use of
->(arrow) operator?
The arrow ‘->‘ operator is used to
access members of a structure through a pointer to the structure.