-1
struct people
{
    char *name;
    char *surname;
} *human;
human = malloc(10*sizeof(struct people));

Hello everyone. I am trying to acccess elements of this struct array but I think I am doing it wrong.
I tried this to access second element's name string
human[1].name;
And when it didn't worked I tried this
human[1.sizeof(struct people)].name;
Thanks in advance

ossobuko
  • 851
  • 8
  • 25

1 Answers1

1

declare the struct and the array that wiill be simpler

typedef struct people
{
    char *name;
    char *surname;
} people;

people  * human=malloc(10*sizeof(struct people));

humam[0].name=malloc((10+1)*sizeof(char));

strcpy(human[0].name,"John");

Don't forget to use malloc for the surname and name because the struct only contains pointers

Gabriel
  • 3,564
  • 1
  • 27
  • 49