tags:

views:

75

answers:

1

Hello,

gcc

I am just getting back into c programming and I am just practicing with structures. However, I have a nested structure that I want to fill from another initialized structure. However, I don't think my technique is correct way to do this.

Any advice would be most helpfull,

#include <stdio.h>

typedef struct
{
  char name[20];
  int age;

} NAME_AGE_STRUCT;

typedef struct 
{
  int ID;
  NAME_AGE_STRUCT info[];

} DETAILS_STRUCT;


int main(void)
{
  DETAILS_STRUCT details;
  NAME_AGE_STRUCT extra_details [] = {
    { "john", 34 },
    { "peter", 44 },
  };

  printf("=== Start program\n");

  details.ID = 2;
  details.info = extra_details;

  return 0;
}
+5  A: 

You need to specify a length of the array in the DETAILS_STRUCT; otherwise there's no memory to assign into. If you want to have an arbitrary array there, declare it as a pointer instead:

typedef struct 
{
  int ID;
  NAME_AGE_STRUCT *info;
} DETAILS_STRUCT;
unwind