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

restorecrtmode example in c

Header file:     graphics.h Synopsis:        void restorecrtmode();       Description:       restorecrtmode() restores screen mode to text mode. restorecrtmode function in c graphics   #include <graphics.h>   #include <stdlib.h>   #include <stdio.h>   #include <conio.h>   int main(void) {         /* request auto detection */         int gd = DETECT, gmode, err;         int midx, midy;         /* initialize graphics and local variables */         initgraph(&gd, &gmode, "C:/TURBOC3/BGI");         /* read result of initialization */         err = graphresult();         if (err != grOk) {                 /* an error occurred */               ...

How to install JDK and setting environment variables for java

In this tutorial, we are going to see the following. How to install JDK How to run java program without setting java path How to set temporary java path in windows How to set permanent java path in windows How to install JDK? Check whether JDK is installed on your PC.  If it is not installed, please go to the below link and download JDK under Java SE(Java Standard Edition) in oracle website . Install the downloaded JDK in your PC. Once JDK is installed, you can run your java program using any method shown below. Having your java source code under jdk/bin, compile the source code and run the program from same directory Setting temporary path for java, compiling the source code and running the program from any location using command prompt Updating the java path in system or user variable(in system properties), compiling the source code and running the program anywhere using command prompt Let us see how to run java program without setting environmental variables.  Once java is ...

Save image in Microsoft Paint without white background

Here, we are going to see how to save image in mspaint without white background 1. Create your own drawing or painting 2. Crop the needed portion of your drawing or painting or image. Here, I am going to crop the porting inside the red box. Now, we have cropped the portion that needs to be saved as an image. 5. Clear the screen and paste the image which we cropped earlier. 6. Decrease the screen size by 50% 7. Now, adjust your screen to fit to your image size. 8. Save the image to desired file format(jpeg, png, bmp etc) 9. Go to the saved location and open the file.  Below is the output image of mine Hope you liked this article!!  For any suggestions, please comment below.