C examples for Structure:Structure Definition
Arrays of Structures
#include <stdio.h> #include <ctype.h> typedef struct Horse Horse; // Define Horse as a type name struct Horse // Structure type definition { int age;// w ww.ja v a 2s.c o m int height; char name[20]; char father[20]; char mother[20]; }; int main(void){ Horse my_horses[50]; // Array of Horse elements int hcount = 0; // Count of the number of horses char test = '\0'; // Test value for ending for(hcount = 0 ; hcount < sizeof(my_horses)/ sizeof(Horse) ; ++hcount) { printf("another (Y or N)? "); scanf(" %c", &test, sizeof(test)); if(tolower(test) == 'n') break; printf("Enter the name of the horse: " ); scanf("%s", my_horses[hcount].name, sizeof(my_horses[hcount].name)); printf("How old is %s? ", my_horses[hcount].name ); scanf("%d", &my_horses[hcount].age); printf("How high is %s ( in hands )? ", my_horses[hcount].name); scanf("%d", &my_horses[hcount].height); printf("Who is %s's father? ", my_horses[hcount].name); scanf("%s", my_horses[hcount].father, sizeof(my_horses[hcount].father)); printf("Who is %s's mother? ", my_horses[hcount].name); scanf("%s", my_horses[hcount].mother, sizeof(my_horses[hcount].mother)); } // Now tell them what we know. printf("\n"); for (int i = 0 ; i < hcount ; ++i){ printf("%s is %d years old, %d hands high,", my_horses[i].name, my_horses[i].age, my_horses[i].height); printf(" and has %s and %s as parents.\n", my_horses[i].father, my_horses[i].mother); } return 0; }