Quantcast
Channel: User Schwern - Stack Overflow
Viewing all articles
Browse latest Browse all 581

Answer by Schwern for why can init array in struct with array-name in C

$
0
0

I'm not entirely sure what you're asking, but here's a go.

.x = y is there for the convenience of the programmer. It's so they don't have to remember the exact ordering of the declaration of the struct, and so the struct members can be reordered without breaking code.

These all do the same thing.

    Man man1 =     {        .name = "Bob",        .age = 18    };    Man man2 =     {        .age = 18,        .name = "Bob"    };    Man man3 =     {"Bob",        18    };

They all say to assign "Bob" to the char name[10] field, and to assign 18 to the char age field.

Demonstration.

    Man man4 =     {        18,"Bob"    };

man4 is different. The order of the arguments matters. man4 assigns 18 to the char name[10] field and "Bob" to the char age field resulting in undefined behavior.

Demonstration.


Viewing all articles
Browse latest Browse all 581

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>