DataStructure In C: Understanding Structure With Example

Structure  is to organizing information together. As we have learned about Array in C, Array is also organizing data value sequentially but the restriction is data should of same type. Click Here For More On Array

Structure is the solution it can organize different kind of datatype. It is very useful where we want to keep whole information in same memory place of any entity.

Note: Entity is anything that has its own existence, example a person, animal, car,
bike,pen etc. it is also known as object

Structure is defined by struct keyword, and the entity name for which we are storing information. For example we want to store data for any student like name,age and percentage. 

Structure defined within the curly braces and it will be ended with ";" 


struct stud {


char name[10];


int age;


float percentage;

};
In the above example stud (name of structure) is the structure holding name (char array), age (int) and percentage (float) values of any student.

But we would not be able to use this structure to storing information because we need a variable that will represent the "stud" in program. There are two ways to define structure variable.

1. Define variable after closing curly brace.

2. Define as struct structure_name variable_name

Variable will represent structure so that it will have access to call data values defined within the structure by using " . "

variable.dataValue example - s.name, s.age, s.percentage



#include <stdio.h>


void main () {


struct stud {


char name[10];


int age;


float percentage;

}s,e,r;


//Another way to create variable

struct stud s2;


printf("Enter 1st student name, age and percentage\n");


scanf ("%s %d %f",s.name, &s.age, &s.percentage);


printf("\n\nEnter 2nd student name, age and percentage\n");


scanf ("%s %d %f",s2.name, &s2.age, &s2.percentage);


printf("\n\nStudents detail - \n ");


printf ("%s ,  %d,   %f \n\n ",s.name, s.age,s.percentage);


printf ("%s ,  %d,   %f  ",s2.name, s2.age,s2.percentage);


}

Output - 

Enter 1st student name, age and percentage
John 15 77


Enter 2nd student name, age and percentage
Rex 6 76


Students detail -

John ,  15,   77.000000 

Rex ,  6,   76.000000 


You may have a question "which approach is best defining structure variable with structure body or creating when needed." 

Answer is the second one because the best idea to create when it is needed in the program e,r are not using but both are consuming memory.

Memory Allocation Of Structure 

Structure contains collection of different datatypes so that it's variable will allocate memory for all. Taking example of program given above.

Structure stud holding 10 bytes space for name , 2 or 4 bytes for age and 4 bytes for float. Click Here For More On DataTypes In C

Note: - int datatype size depends on system architecture it may be 2 or 4.

Let's see what would be memory size of structure stud.


Structure In C
Structure Memory Allocation. ByTechAchievers.com


#include <stdio.h>


void main() {

  
struct stud {


  char name[10];


  int age;


  float percentage;


};


struct stud st;


printf("Size of name - %d\n\n",sizeof(st.name));


printf("Size of age - %d\n\n",sizeof(st.age));


printf("Size of percentage - %d\n\n",sizeof(st.percentage));


printf("Size of structure - %d\n\n",sizeof(st));


}


Output -


Size of name - 10


Size of age - 4


Size of percentage - 4


Size of structure - 20
Here you can see that structure's variable allocations size is  10 + 4 + 4 = 18 bytes and  the structure size is 20 which is 2 more bytes extra. According to system's architecture (32 bit or 64 bit) it differs.  it is called Padding in structure. 

Padding is the extra memory space taken by structure for other structure member in advance, It increase the performance of system.


compiler does default padding we can also set padding explicitly but it is recommended to go with default one. Let's see the same program with user-defined padding in structure.

#include <stdio.h>

/* 
#pragma pack(1) :

Tells to compiler to align all data members 
of structure within their memory size */


#pragma pack(1)


void main() {

  
struct stud {


  char name[10];


  int age;


  float percentage;


};


struct stud st;


printf("Size of name - %d\n\n",sizeof(st.name));


printf("Size of age - %d\n\n",sizeof(st.age));


printf("Size of percentage - %d\n\n",sizeof(st.percentage));


printf("Size of structure - %d\n\n",sizeof(st));


}

Output -


Size of name - 10


Size of age - 4


Size of percentage - 4


Size of structure - 18
Structure With Pointer

Pointer is a variable that contains address of another variable and can make any change in variable's memory. I am assuming you are aware about pointer how it works if you are new to pointer please go through with this link. Pointer in C

Pointer in structure denotes structure variable using "->" operator. Pointers contains structure's address not its child's variables. I have explained how to use pointers with structure in other chapter please go through with this link for examples.

How To Use Pointer In Structure 

Thanks for reading, please share your comments and suggestions in comment section.

No comments

Powered by Blogger.