structures
structures
STRUCTURE
In basic terms we refer to structure as a collection of heterogeneous data elements. Structures are
basically used to collect information related to single entity as one collective unit. We group a number
of variables of heterogeneous data types under one structure which then can be accessed through the
objects of that structure. They act like classes with all members defined with default public access
specifier. Syntax struct struct_name
{
type1 member1; type2 member2; typen membern;
} obj1, obj2;
In C, struct keyword is used to declare a structure followed by the name of the structure. This name
must be defined if we would like to make multiple copies of our structure just like any other data type
in C programming language. Simple objects or array of objects can also be declared for creating
instances of structure.
Example
struct student
{ char name[30]; int age; int rollno; float marks;
} s1, s2;
In this case, we have declared a structure bearing “student” name. In this structure we have declared 4
members inside the body. Total of 2 objects have also been created namely “s1 and s2”.
2. struct student
{ char name[30]; int age; int rollno;
}; student s1,s2; // objects of the structure declared here Second way to create objects of the structure
is to first create the structure and then later on you can create the objects of that structure just like
declaring any other simple variables.
3.
struct student
{ char name[30]; int age; int rollno;
};
student s1{“dinesh”,27,101}; // object declared here
In this case as you can see that while creating the object of that structure we have also initialized the
members of the structure.
Now there are three data members of the structure student. As we initialize the members of the objects,
following values will be assigned to each member.
Name Dinesh
Age 27
Rollno 101
Table 8.2 Initialization of structure
obj1.age
As we can see that there are three members of the structure student. When we create 2 objects of that
structure, separate copy of all the three members will be allocated to each object. Explanation obj1.age
= 27; obj2.age = 28;
You can see that we are trying to assign value to the member age twice. But the value will not be
overwritten as member age belongs to two different objects and hence the memory allocated to them is
also separate.
Array uses subscripts or “[ ]” (square Structure uses “.” (Dot operator) for
possible. possible.
4
ARRAY STRUCTURE
Array size is fixed and is basically the Structure size is not fixed as each
Structure is a user-defined
struct sruct_name{
data_type1 ele1;
data_type2 ele2;
data_type array_name[size]; };
UNIONS
Union is another user defined data type like structure which is capable of grouping together members of
heterogeneous data type under one name. Beside many similarities between structure and union, there
6
are operational differences between the two. All of the members of a union share the same memory
space.
After declaring a union object, instead of allocating memory for each member variable, the compiler
assigns an amount of memory equal to the largest variable. Example of structure, structure citizen
{ char name[30]; char idno[20];
};
Memory allocated to the structure is as follows:-
Name Idno Total
30 bytes 20 bytes 50 bytes
Table 8.3 memory allocated to structure Example of union, union citizen // keyword union
to declare union
{ char name[30]; char idno[20];
};
Memory allocated to the union is as follows:-
Name Idno Total
30 bytes - 30 bytes
1. The union keyword is used to declare a union. The struct keyword is used to declare a
structure.
2. Memory allocated to union is equal to the
memory that’s needs to be allocated to its
Members of the structure are allocated
largest member.
memory depending upon their data type, so
memory allocated is flexible. You don’t have
to declare the objects of structure at the time of
3. Union is preferred when only one member of declaring structure.
the union needs to be accessed at one time.
4. Members of the union can’t be initialized as Structure is preferred when we there is a need
they share memory. Value of the latter to access all the members of structure at same
former member
Members of the structure can be initialized as
they do not share memory and all the members
can be accessed at same time