Basics of C Language Structure.

Let’s understand structure concepts with interview questions and programming examples step by step.

Why do we need structure? What is the difference between an Array and a Structure?

– An array is the collection of the same type of data.

int arr[100];

– int arr[100], contains data of 100 integer types.
– let’s say we need to declare a data type to store a roll number of students. An array can be useful. But, if we want to store the name and roll number of students then only the array may not fulfill the requirements.
– If the programmer needs the collection of different types of data then? That is where structure comes into consideration.
– Array and Structure are abstract data types. But, the array contains the same type of data. And, a structure contains mixed types of data.

How to define a structure?
struct <name>
{
     data_type mem1;
     data_type mem2;
     ..............
     ..............
     data_type memN;
};

ex:
struct ek
{
     int i;
     float f;
     double d;
     char c;
};

– struct is the keyword that tells the compiler that structure is defined. Members can be mixed types of data.
– In the above example, the defined structure has members ‘i’ of integer type, ‘f’ of float type, ‘d’ of double type, and ‘c’ of character type.
– Defining structure is just a definition. it doesn’t hold any memory.

Declare a structure variable.
struct ek st;

– Declared structure variable st of type struct ek. ‘ek’ is also called as a structure tag.
– As soon as the structure variable is declared. it will reserve space in the memory. Variable st of type struct ek consumes memory as shown below.

//considering 64-bit machine.
struct ek
{
     int i;           // 4 bytes
     float f;       // 4 bytes
     double d;  //8 bytes
     char c;      //1 byte
};

struct ek st; //reserves 4+4+8+1 = 17 bytes.

– We can also declare structure variables while defining structure as shown below.

struct ek
{
     int i;           // 4 bytes
     float f;       // 4 bytes
     double d;  //8 bytes
     char c;      //1 byte
}st;
Structure with typedef.
typedef struct ek       //defining the structure with typedef
{
     int i;
     float f;
     double d;
     char c;
}embeddedkernel;

embeddedkernel st;  //declraing the structure variable using typedef name

– Now, the structure is identified with another name embeddedkernel. And that name is used to declare structure variables as shown in the above example.

Structure Variable Initialization.

– Structure defined and declared. Let’s initialize the structure ek declared above.
– In the below example, Initializing structure variable.

struct ek
{
     int i;           
     float f;      
     double d; 
     char c;      
};

struct ek st = {1, 1.1, 1.2, 'A'};

– In the below example, Defining structure, Declaring a variable of structure, and Initializing the structure altogether.

struct ek
{
     int i;        
     float f;       
     double d; 
     char c;      
}st = {1, 1.1, 1.2, 'A'};

– The first value is assigned to the first member variable, the second value is assigned to the second member variable, and so on. The order of values while initialization is important here. Now, structure variable st’s members having values, i = 4, f = 1.1, d = 1.2, c = ‘A’.

– If all the members of structures are not initialized then others will be assigned to value ‘0’ automatically as shown below.

struct ek
{
     int i;         
     float f;      
     double d;  
     char c;    
}st = {1, 1.1};

– Now, structure variable st’s members having values, i = 4, f = 1.1, d = 0, c = 0.

Assign a value to a specific member variable without worrying about order.
struct ek
{
        int i;
        float f;
        double d;
        char c;
};

struct ek st =
{
      .c = 'A'
      .i = 1,      
};

– Now, structure member variable ‘i’ has value 1. ‘c’ has the value ‘A’. And others have the value ‘0’. And no need to worry about the order of the value while initialization.

Assign all the structure members to value ‘0’.
struct ek
{
     int i;          
     float f;       
     double d;  
     char c;     
}st = {0};
struct ek
{
     int i;           
     float f;      
     double d; 
     char c;      
};

struct ek st = {0};

– Now, structure variable st’s members having values, i = 0, f = 0, d = 0, c = 0.

– Structure member’s initialization is not allowed in the structure definition.

struct ek
{
     int i;          
     float f = 1.1;        //Not valid     
     double d;  
     char c='B';        //Not valid
}st;
Accessing member variables of the structure.

– Dot operator is used to access the structure member variable.

structure_variable_name.structure_member_variable_name;
#include<stdio.h>          
struct ek                      //declaring the strcuture
{
        int i;
        float f;
        double d;
        char c;
};

struct ek st = {1, 1.1, 1.2, 'A'};      //creating structure variable and initializing structure st member variables.

int main()
{
        printf("%d %f %f %c\n", st.i, st.f, st.d, st.c); //accessing structure's member variables using dot operator.
}

output: 
1 1.100000 1.200000 A
How structure is stored in the memory?
#include<stdio.h>
struct ek
{
        int i;
        float f;
        double d;
        char c;
};

struct ek st = {1, 1.1, 1.2, 'A'};

int main()
{
        /*printing addresses of structre st and member varables*'
        printf("%p %p %p %p %p\n", &st, &st.i, &st.f, &st.d, &st.c);
}

output:
0x100310000 0x100310000 0x100310004 0x100310008 0x100310010

– The program ran on a 64-bit system.
– st’s address is 0x100310000, which is the start of the structure address.
– i’s address is the same as st’s address because it is the first member variable stored at the start of the structure memory.
– Member variable integer ‘i’ size is 4 bytes. So, ‘f’ is stored at (address of i) + 4 = (0x100310000) + 4 = 0x100310004.
– ‘f’ is also 4 bytes. So, ‘d’ is stored at (address of f) + 4 = (0x100310004) + 4 = 0x100310008.
– Member variable ‘d’ is double(size 8 bytes). So, ‘c’ is stored at 0x100310008 + 8 = 0x100310010 (remember, values are shown in hex).

– What did you observe? – structure’s memory allocation is contiguous same as array.

Leave a Comment