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

前端 未结 3 628
孤独总比滥情好
孤独总比滥情好 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.)

    0 讨论(0)
  • 2021-02-09 16:29

    A struct declaration tells the compiler how a struct looks like and optionally gives that struct a name. If you want to use a struct as a "type" in your code, it requires a name:

    struct coordinate {
      int x;
      int y;
    };
    
    // Can now be used as a return type:
    struct coordinate getCoordinate ( ) { ... }
    
    // Can now be used as an argument:
    void drawAtPoint ( struct coordinate point ) { ... }
    
    // Can be used to create new constants or variables:
    const struct coordinate kOrigin = { 0, 0 };
    struct coordinate lastViewPosition;
    

    Alternatively you can create a nameless struct and explicitly define it to be a new type using a type definition:

    typedef struct {
      int x;
      int y;
    } coordinate;
    
    // Can now be used as a return type:
    coordinate getCoordinate ( ) { ... }
    
    // Can now be used as an argument:
    void drawAtPoint ( coordinate point ) { ... }
    
    // Can be used to create new constants or variables:
    const coordinate kOrigin = { 0, 0 };
    coordinate lastViewPosition;
    

    But if you don't do either one, you cannot use that struct as a type since in C, the type of a struct is defined by its name and not by its data layout.

    The following is valid C code:

    struct coordinate {
      int x;
      int y;
    };
    
    struct coordinate startCoord = { 20, 80 };
    struct coordinate endCoord = startCoord;
    

    Yet the following is not:

    struct {
      int x;
      int y;
    } startCoord = { 20,  80 };
    
    struct {
      int x;
      int y;
    } endCoord = startCoord;
    

    Compiler will fail with an error. It seems as if startCoord and endCoord have the same type in both examples, but that's not true. Two structs don't denote the same type just because they have an equal memory layout or equal field names. For a C compiler, startCoord and endCoord have different types in the later example and thus you cannot assign them as shown above as this requires that both of them have the same type.

    So the only situation where you can skip naming your structs is when you directly declare variables of that struct type and there is no requirement to ever refer to that struct again as a data type throughout your code.

    0 讨论(0)
  • 2021-02-09 16:30

    In the first case :

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

    you declare a type with name struct st1, and you also create two objects, of this type, x and y. So you can create objects of this type whenever you want, like this :

    struct st1 obj1;
    

    However in the second case :

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

    you create a struct and two objects, x and y, but you cannot access this struct again. This means that you cannot create any new objects of this type.

    0 讨论(0)
提交回复
热议问题