-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!!

3
  • Person people[N] to allocate in automatic memory or Person *people = malloc(N * sizeof *persons) for dynamic. Commented Apr 28, 2014 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). Commented Apr 28, 2014 at 21:27
  • Array in C Commented Apr 28, 2014 at 21:28

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.