Structure
Structure
2. Differentiate between structure and union by citing suitable examples? Discuss their
respective usage? 15
3. Give examples of user defined data types using a structure? What are the differences
between array and structure? 7
4. What do you mean by structures? How are structures declared and initialized? Ex-
plain with examples? How are structures different from arrays? 15
5. What do you mean by structures? How are structures declared and initialized? Ex-
plain with examples? How are structures different from unions? 15
Structures in C
Structures (structs):
A structure is a collection of data items, usually of different data types which are com-
bined into a single unit.
For example: telephone directory as it consist of name, address and telephone number.
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.
Syntax:
struct_structure_name
{
data_type_variable_name;
data_type_variable_name;
........
data_type_variable_name;
};
For Example:
struct student
{
int roll_no;
char name[20];
int age;
float marks;
};
Create a Structure
You can create a structure by using the struct keyword and declare each of
its members inside curly braces:
Example
// Create a structure
struct myStructure {
int myNum;
char myLetter;
char myString[30];
};
int main() {
// Create a structure variable and assign values to it
struct myStructure s1 = {13, 'B', "Some text"};
// Print values
printf("%d %c %s", s1.myNum, s1.myLetter, s1.myString);
return 0;
}
Output:
13 B Some text
Difference Between Structure and Union in C
Parameter Structure Union
Keyword A user can deploy the keyword struct to de- A user can deploy the keyword union to define a
fine a Structure. Union.
Internal Implemen- The implementation of Structure in C occurs In the case of a Union, the memory allocation oc-
tation internally- because it contains separate mem- curs for only one member with the largest size
ory locations allotted to every input member. among all the input variables. It shares the same
location among all these members/objects.
Accessing Mem- A user can access individual members at a A user can access only one member at a given
bers given time. time.
Syntax The Syntax of declaring a Structure in C is: The Syntax of declaring a Union in C is:
union [union name]
struct [structure name]
{
{
type element_1;
type element_1;
type element_2;
type element_2;
.
.
.
.
} variable_1, variable_2, …;
} variable_1, variable_2, …;
Size A Structure does not have a shared location A Union does not have a separate location for ev-
for all of its members. It makes the size of a ery member in it. It makes its size equal to the
Structure to be greater than or equal to the size of the largest member among all the data
sum of the size of its data members. members.
Value Altering Altering the values of a single member does When you alter the values of a single member, it
not affect the other members of a Structure. affects the values of other members.
Storage of Value In the case of a Structure, there is a specific In the case of a Union, there is an allocation of
memory location for every input data member. only one shared memory for all the input data
Thus, it can store multiple values of the vari- members. Thus, it stores one value at a time for
ous members. all of its members.
Initialization In the case of a Structure, a user can initialize In the case of a Union, a user can only initiate the
multiple members at the same time. first member at a time.
Difference between Array and Structure
The following are the important differences between array and structure −
Definition It is a type of data structure in the form of a It is a type of data structure that works as a
container that holds variables of different container to hold variables of the very same
types. type. Array does not support variables of mul-
tiple data types.
Allocation of In a structure, the memory allocation for The array stores the input data in a memory
Memory the input data doesn’t require being in con- allocation of contiguous type. It means that
secutive memory locations. the array stores its data in a type of memory
model where the memory blocks hold con-
secutive addresses (it assigns memory
blocks consecutively).
Accessibility For a user to access the elements present On the other hand, any user can easily ac-
in a structure, they require the name of cess the elements by index in an array’s
that particular element (it is mandatory for
retrieval). case.
Pointer A structure holds no concept of internal An array, on the other hand, implements
Pointer. Pointer internally. It always points at the very
first element present in the array.
Instantiation One can create an object from the struc- An array does not allow the creation of an ob-
ture after a later declaration in its program. ject after the declaration.
Types of Data A structure includes multiple forms of data- A user cannot have multiple forms of data-
Type Variables type variables in the form of input. type variables in an array because it supports
only the same form of data-type variables.
Performance A structure becomes very slow in perfor- The process of searching and accessing ele-
mance due to the presence of multiple ments is much faster in the case of an array
data-types. The process of searching and due to the absence of multiple data-type vari-
accessing elements becomes very slow in ables. It is, thus, better and faster in perfor-
these. mance.
element type 1;
element type 2;