arrayscstruct

why can init array in struct with array-name in C


Bellow code is for 'init array element in strcut'

(updated) after reading the comments/answers, now I know this format is 'Designated initialization' in C99

typedef struct 
{
    char name[10];
    char age;
}Man;


void main()
{

    Man man1 = 
    {
        .name = "Bob", // why not .name[10] = "Bob",
        .age = 18
    };

    printf("%s\n", man1.name); // "Bob"
    printf("%d\n", man1.age);  // 18

}

Solution

  • I have doubt about .name, why it is not .name[10] = "Bob"

    It is unclear to me why you think it should be .name[10] = "Bob", so I will try to answer it in a few different ways.