0
struct data
{
    char name;
    int conn[3];
};

typedef struct data unit;
typedef unit *link;

int main()
{
    int i;
    link p[100];
    for(i=0;i<=100;i++)
    {
        p[i]=(link)malloc(sizeof(unit));
        p[i]->name='h';
        p[i]->conn[]=(int*){"1","1","1"};   **// assignment error**
    }
    for(i=0;i<=100;i++)
    {
        printf("%c\t%d\t%d\t%d\n",p[i]->name,p[i]->conn[0],p[i]->conn[1],p[i]->conn[2]); 
    }
    getch();
}

As structures does not support initialization so, is there any way to assign this type of arrays declared inside a structure in a single line of code without using mem allocation functions and all for the sake of simplicity? Please stick to the code.

Mike
  • 4,041
  • 6
  • 20
  • 37

1 Answers1

5

After the malloc line you can write:

*p[i] = (struct data){'h', {1, 1, 1}};

This uses structure assignment with a compound literal as the source.

BTW I would recommend doing the malloc line this way: p[i] = malloc(sizeof *p[i]);, see here for explanation.

M.M
  • 138,810
  • 21
  • 208
  • 365