What is the use of Struct Tag name in C programming?

前端 未结 3 629
孤独总比滥情好
孤独总比滥情好 2021-02-09 15:39

I want to know the actual use of struct tag_name in C programming. Without using the tag_name also i was getting output as with use of tag_name. I want the exact reason behind t

3条回答
  •  失恋的感觉
    2021-02-09 16:24

    If you use the tag, you can create more variables of type struct st1 (or types derived from that (e.g., struct st1*)) later:

    struct st1 z, *zp;
    

    while after:

    struct { int x; char c;}x={100,'a'},y={70,'e'};
    

    you can never create variables of this type ever again.

    While you can do:

    struct { int x; char c;} z = {80, 'f'}; 
    //same layout and member names
    

    it will have a type that's different from that of x's and y's as far as aliasing and type checking is concerned.

    The tag allows you to reuse the type.

    (Typedefing an anomyous struct

    typedef struct { int x; char c;} st1;
    st1 x,y,z;
    

    is another way to reuse the type.)

提交回复
热议问题