Sep 5, 2015

C Programming #53: Structure - introduction

Structures in C are very important in organizing/storing data. It helps in programmer create their own data type too. Lets take a example to explore the need and elegance of Structure in C.



Say I want to store name, age and class of 5 students.

Name  Age  Class
Ram   7    2
Mason 9    4
Akbar 5    1
Emma  10   5
David 14   9

Now to store all this data we need variables. Lets analyse the type of variable that we would need. Name is a string and can be stored in array of characters. Age is always a non negative number and can be stored in unsigned integer. Class is also a non negative number and can also be stored in unsigned integer. Since there are five students whose details have to be stored it makes sense to create a array.


#include <stdio.h>

int main()
{
   char name[5][20] = {"Ram", "Mason", "Akbar", "Emma", "David"};
   int  age[5] = {7, 9, 5, 10, 14 };
   int  class[5] = {2, 4, 1, 5, 9};
 
   int i = 0;

   for(i = 0; i < 5; i++) {
      printf("%s is %d years old and studies in %d Class\n", 
             name[i], age[i], class[i]);
   }
   return 0;
}

Output of above program is


Ram is 7 years old and studies in 2 Class
Mason is 9 years old and studies in 4 Class
Akbar is 5 years old and studies in 1 Class
Emma is 10 years old and studies in 5 Class
David is 14 years old and studies in 9 Class


Yes, program works as expected. It would be very nice to store name, age, class all in single variable since all are related to single student. Before such a variable is created we need to tell to compiler the blue print of it. Following is the definition of a new structure named student.

struct student {
   char name[20];
  int age;
  int class;
};

Note above C statement is called as definition or prototype of a new structure called student. No variable is created, nor memory is allocated, we just are trying to convey to C compiler how would a student would look like. name, age and class are called as member variables of structure.

Lets see how structure variable is created initialized.


#include <stdio.h>

struct student {
   char name[20];
   int age;
   int class;
};

int main()
{
   struct student a = {"Ram", 7, 2};
   struct student b = {"Mason", 9, 4};

   printf("Student %s age is %d and studies in %d\n",
          a.name, a.age, a.class);
   printf("Student %s age is %d and studies in %d\n",
          b.name, b.age, b.class);

   if(a.age > b.age) {
      printf("%s is older\n", a.name);
   }else if(a.age < b.age) {
      printf("%s is older\n", b.name);
   }else {
      printf("%s and %s are both of same age\n",
             a.name, b.name);
   }
 
   return 0;
}

Output of above program is

Student Ram age is 7 and studies in 2
Student Mason age is 9 and studies in 4
Mason is older

Structure variable are declared as follows
struct student a;

Intial values are passed in the order in which memeber variable occur.
struct student a = {"Ram", 7, 2};

Structure members are accessed as a.name, a.age and a.class.

Now lets re-write earlier program using structure.


#include <stdio.h>

struct student {
   char name[20];
   int age;
   int class;
};

int main()
{
   struct student s[5] = {{"Ram", 7, 2},
                          {"Mason", 9, 4},
                          {"Akbar", 5, 1},
                          {"Emma", 10, 5},
                          {"David", 14, 9}};
   int i = 0;

   for(i = 0; i < 5; i++) {
      printf("%s is %d years old and studies in %d Class\n",
             s[i].name, s[i].age, s[i].class);
   }
   return 0;
}


Output of above program is

Ram is 7 years old and studies in 2 Class
Mason is 9 years old and studies in 4 Class
Akbar is 5 years old and studies in 1 Class
Emma is 10 years old and studies in 5 Class
David is 14 years old and studies in 9 Class


Lets go further and analyse how the structure is stored in memory

#include <stdio.h>

struct student {
   char name[20];
   int age;
   int class;
};

int main()
{
   struct student a = {"Ram", 7, 2};

   printf("Sizeof structure student is %d\n", sizeof(struct student));

 
   printf("Address of a is %p\n",&a);
   printf("Address of a.name is %p\n", &a.name);
   printf("Address of a.age is %p\n", &a.age);
   printf("Address of a.class is %p\n", &a.class);
 
   return 0;
}

Output of above program is

Sizeof structure student is 28
Address of a is 0xbfd05694
Address of a.name is 0xbfd05694
Address of a.age is 0xbfd056a8
Address of a.class is 0xbfd056ac
Structure memory layout is as follows


Links

Next Article - C Programming #54: Structure pointer
Previous Article - C Programming #52: Passing array to a function
All Article - C Programming

No comments :

Post a Comment