Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Question_1: How to declare an array of Structure?

Give a detailed
explanation with examples of arrays of a Structure. (Discuss the declaration,
initialization and accessing of elements using Array of Structure)

Array of structure:
An array of structres in C can be defined as the collection of multiple structures
variables where each variable contains information about different entities.
 The array of structures in C are used to store information about multiple
entities of different data types.
 The array of structures is also known as the collection of structures.
Declaring an array of structure:
Declaring an array of structure is same as declaring an array of fundamental types.
Since an array is a collection of elements of the same type. In an array of
structures, each element of an array is of the structure type.

Example of Array of Structure:


Declaration:

Here,we have used this “struck” keyword to declare the structure variable. Then
within the curly braces we’ve the definition of the variable “car”, that is different
elements of car structure.

In structure, data type is <struct name>. So, the declaration will be

<struct name> variables;

Example

struct car car1;

Initialization:
We can initialize the structrue members directly like below,

Example

Accessing of Structure members:

To access structure members, we have to use dot (.) operator.


It is also called the member access operator.

To access the price of car1,

car1.price

To access the name of car1,


car1.name

Here if I want to access the members of the structure then I


need to use dot (.) operator as usual.

The output of the above program is given below:

Thus is this way, we can declare,initialize & access the members of array of
structure.
Question_2: Elucidate the process of Structure Padding with
diagrams and examples.

Structure Padding:

When we create the structure or union then compiler inserts some extra
bytes between the members of structure or union for the alignment.
These extra unused bytes are called padding bytes and this technique is
called structure padding in C.

 Padding increases the performance of the processor at the penalty


of memory.
 In structure or union data members aligned as per the size of the
highest bytes member to prevent the penalty of performance.
 The structure padding is automatically done by the compiler to
make sure all its members are byte aligned.

Let’s see some examples and diagrams below:

Example-1:

The size of the below structure is 8 bytes due to structure padding:


Here ‘char’ is only 1 byte but after 3 byte padding, the number starts at 4
byte boundary. (For ‘int’ and ‘double’, it takes up 4 and 8 bytes
respectively.)

The compiler used the 3 wasted bytes (marked in red) to pad the
structure so that all the other members are byte aligned. Now, the size of
the structure is 4 + 1 +3 = 8 bytes. The size of the entire structure is 8
bytes. On knowing the structured padding, it is easier to redesign or
rewrite the structure.

Let’s see another example:

Example-2:
Here, in first part ‘‘var variable, “int’ is 4 bytes, ‘double’ is 8 bytes and
‘float’ is 4 bytes.In the 64-bit memory processor, in 1 CPU cycle it
contains 8 bytes for which the first 4 bytes of ‘int’ will be placed first.
Then the size of ‘double’ is 8 bytes which cannot be taken in the same
CPU cycle for which we can see here 4 wasted bytes marked with red )
to pad the structure so that all the other members are byte aligned in first
CPU cycle.Then in second CPU cycle , size of double which is 8 bytes
are taken and lastly we have 4 bytes of float which will be taken in 3rd
CPU cycle.When processor will try to access this float of 4 bytes, the
whole word will be accessed and all the 8 bytes will be considered.Now,
the size of the structure is 4 bytes + 4 bytes + 8 bytes + 4 bytes + 4
bytes = 24 bytes. The size of the entire structure is 24 bytes.
Here, in second part,at first we have ‘double’ whose size is 8 bytes.So
in 64-but memory processor, double will take 8 bytes in 1st CPU cycle.
Then we have ‘int’ data type whose size is 4 bytes & ‘float’ whose size
is also 4 bytes..This two will access the 8 bytes in the 2nd CPU cycle &
fill up them..So no wasted bytes will remain in this case. Now, the size
of the structure is 8 bytes + 4 bytes + 4 bytes = 16 bytes. The size of the
entire structure is 16 bytes.

The output of the given program-2 will be thus:

You might also like