Skip to main content

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);
}
printf("\n");
return;
}

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");
printDetails(arr1);

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

/* printing the contents of partially initialized array arr3 */
printf("Contents of arr3:(arr3[1] - partially initialized)\n");
printDetails(arr3);
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");
printDetails(arr3);

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

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Contents of arr1:(uninitialized global variable)
  Name: 
  Age: 0 Rollno: 0
  Name: 
  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



Comments

Popular posts from this blog

Array of pointer to structure

Array of pointer to structure is nothing but an array whose elements are pointers to structure How to declare array of pointer to structure? Below is an example declaration for array of pointer to structure. struct student *arr[10]; Here, arr is an array 10 of pointer to structure student. Dynamic memory allocation: Below is the procedure to perform dynamic memory allocation for the structure pointers in an array. struct student { int age; char name[32]; }; fun() { int i = 0; struct student *arr[10]; for (i = 0; i < 10; i++) { /* dynamic memory allocation for 5 structure objects */ arr[i] = (struct student *)malloc(sizeof(struct student) * 5); } } Write a c program to pass an array of structure pointers to another function   #include <stdio.h>   #include <stdlib.h>   #include <string.h>   struct student {         int age, rollno;         char name[32];   };   /* printing details of stude...

Nested function in C

What is nested function? If a function is defined inside another function, then it is called as nested function.  Nested function example in C: Consider the following example, int add(int a, int b) {        void print(int res) {          printf("Result is %d, res);       }       print(a+b);       return 0; } Here, print() is a nested function.  Because, it is defined inside another function named add() . Example C program using nested functions: #include <stdio.h> void add(int a, int b) { void print(int res) { // nested function printf("Result is %d\n", res); return; } print(a + b); // passing sum of a and b as parameter return; } int main() { add(10, 20); return 0; }   Output:   jp@jp-VirtualBox:~/$ ./a.out   Result is 30 Previous Next

Difference between asterisk and ampersand operators in c

Address Operator (& - Ampersand): Returns address of the given variable.  Consider the following example, int *ptr, var = 10; ptr = &var; ptr = &var sets the address of the variable var to pointer ptr.  & is also called as reference operator. Let us try to understand the purpose of reference operator using the following example program.   #include <stdio.h>   int main() {         int var = 10, *ptr;         /* assigning address of variable var to pointer ptr */         ptr = &var;         /* printing the address of the variable var */         printf("Address of var is 0x%x\n", &var);         /* printing the value of pointer ptr */         printf("Value of ptr is 0x%x\n", ptr);         return 0;   }   Output:   jp@jp-VirtualBox:~/$ ./a.out   Address of var is 0x...