Home » Uncategorized » Array of structures in C

Array of structures in C

Below is a sample program explaining the array of structures in C. This tutorial is meant for beginners and has a very simple syntax to try.

#include <stdio.h>
#include <string.h>

// TODO : this program doesnt work, fixit

int main(void) {
	struct person {
		char name[20];
		int age;
		float salary;
	};

	struct person p[5];
	int i = 0, j = 0;
	char ch, temp[20];

	for (i=0; i<5; i++) {
		printf("\nEnter %d persons name, age & salary: ", i);

		j = 0; //reset for taking new name
		while ((ch = getchar()) != ' ') {
			temp[j] = ch;
			j++;
		}
			temp[j] = '\0';


#if 0
	        fgets (temp, 20, stdin);
		/* Workaround to avoid \n and %s complexity */
#endif
		strcpy(p[i].name, temp);

		// scanf has a serious problem with \n ,hence it wont work.
		scanf("%d, %f", &p[i].age, &p[i].salary);
	}
	
	printf("===== You Entered =====\n");
	for (i=0; i<5; i++) {
		printf("Name: %s, Age: %d, Salary: %f", p[i].name, p[i].age, p[i].salary);
	}
	return 0;
}

We hope this tutorial is of help. In case you have any other suggestions or questions, do let us know in the comments!


Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment