-2

I need to create an array of persons whilst using a struct something like this :-

typedef struct Person

{
     int age;      //needs to be randomly generated
     int height;   //needs to be randomly generated
     int weight;   //needs to be randomly generated
} Person;

but im not sure how to do this with an array like :-

Person[0]

Person[1]

Person[2]

Any tips will be great!!

AntonH
  • 6,359
  • 2
  • 30
  • 40
  • `Person people[N]` to allocate in automatic memory or `Person *people = malloc(N * sizeof *persons)` for dynamic. – Niklas B. Apr 28 '14 at 21:26
  • Your struct would be like `Person persons[10];` and to access `persons[0].age = 10;` (and so on for the other elements of the struct). – AntonH Apr 28 '14 at 21:27
  • [Array in C](https://www.google.com/search?q=array+in+c&oq=array+in+c&aqs=chrome..69i57j69i60l3j0l2.1215j0j1&sourceid=chrome&espv=210&es_sm=122&ie=UTF-8) – Ed S. Apr 28 '14 at 21:28

1 Answers1

0
typedef struct Person
{
     int age;      //needs to be randomly generated
     int height;   //needs to be randomly generated
     int weight;   //needs to be randomly generated
} Person;

// Create an array of 5 Persons
Person persons[5];

// Set the data of the first person.
person[0].age = 25;
person[0].height = 175; // in cm
person[0].weight = 68; // in kg
R Sahu
  • 204,454
  • 14
  • 159
  • 270