This blog is under construction

Wednesday, 8 January 2014

Array of structures

We can create array of structures similar to creating array of any primitive data types. Below is the general form of declaration for array of structure.
struct  <structure_name>  <array_name>[SIZE];

Consider the following example
struct student {
char name[32];
int age, rollno;

struct student arr[2];

Here, arr is an array of 2 structure elements.

Let us see how to initialize an array of structures.

Method 1:
struct student arr[2] = { {"Tom", 10, 101}, {"Jerry", 11, 102} };

Method 2:
strcpy(arr[0].name, "Tom");
arr[0].age = 10;
arr[0].rollno = 101;

strcpy(arr[1].name, "Jerry");
arr[1].age = 11;
arr[1].rollno = 102;

Apart from the above, we are allowed to do partial initialization for structure elements in an array.

Consider the following,
struct student arr[2] = {{"Tom", 10, 101}, {"Jerry"}};

In the above example, we have done partial initialization for second element in the structure array (ie) we have assigned value for the structure member name, whereas the structure members age and rollno are left uninitialized.  We can assign values to those uninitialized structure members whenever it is needed.

arr[1].age = 11;
arr[1].rollno = 101;

If the array of structure is declared as global variable, then structure members in each structure element are initialized to 0 by default.  Similarly, if any structure element is partially initialized(few structure members alone initialized), then the left over structure members are initialized to 0 by default.

Array of structures example in C

  #include <stdio.h>
  #include <string.h>
  struct student {
        char name[32];
        int age, rollno;
  struct student arr1[2];  // global declaration for array of structures

  /* prints the contents of the given array */
  void printDetails(struct student arr[2]) {
        int i;
        for (i = 0; i < 2; i++) {
                printf("Name: %s\n", arr[i].name);
                printf("Age: %d Rollno: %d\n", arr[i].age, arr[i].rollno);

  int main() {
        /* complete initialization */
        struct student arr2[2] = {{"Ram", 10, 101}, {"Raj", 11, 102}};
        /* partial initialization */
        struct student arr3[2] = {{"Jack", 10, 107}, {"Rose"}};
        struct student arr4[2];

        /* initializing array arr4 */
        strcpy(arr4[0].name, "Mike");
        arr4[0].age = 10;
        arr4[0].rollno = 105;
        strcpy(arr4[1].name, "Tyson");
        arr4[1].age = 11;
        arr4[1].rollno = 106;

        /* printing the contents of arr1 */
        printf("Contents of arr1:(uninitialized global variable)\n");

        /* printing the contents of arr2 */
        printf("Contents of arr2:\n");

        /* printing the contents of partially initialized array arr3 */
        printf("Contents of arr3:(arr3[1] - partially initialized)\n");
        arr3[1].age = 10;
        arr3[1].rollno = 108;

        /* printing the contents of the array after above updation */
        printf("printing the contents of arr3 after updation:\n");

        /* printing the contents of arr4 */
        printf("Contents of arr4:\n");
        return 0;

  jp@jp-VirtualBox:~/$ ./a.out
  Contents of arr1:(uninitialized global variable)
  Age: 0 Rollno: 0
  Age: 0 Rollno: 0

  Contents of arr2:
  Name: Ram
  Age: 10 Rollno: 101
  Name: Raj
  Age: 11 Rollno: 102

  Contents of arr3:(arr3[1] - partially initialized)
  Name: Jack
  Age: 10 Rollno: 107
  Name: Rose
  Age: 0 Rollno: 0

  printing the contents of arr3 after updation:
  Name: Jack
  Age: 10 Rollno: 107
  Name: Rose
  Age: 10 Rollno: 108

  Contents of arr4:
  Name: Mike
  Age: 10 Rollno: 105
  Name: Tyson
  Age: 11 Rollno: 106

No comments:

Post a Comment