Typedef struct vs struct? |Definition difference|

后端 未结 1 1649
北海茫月
北海茫月 2021-02-06 10:29

The following blocks are outside of main() and before every function (global scope)

1st block:

struct flight {
    int nu         


        
相关标签:
1条回答
  • 2021-02-06 11:04

    My question is why typedef needs flight to be written a second time at the end of a block?

    When you declare:

    typedef struct flight{
        int number;
        int capacity;
        int passengers;
     }flight;
    

    you actually declare two things:

    • a new structure type struct flight
    • a type alias name flight for struct flight.

    The reason why the type alias name with typedef appears at the end of the declaration like for any ordinary declaration is because for historical reasons typedef was put in the same specifiers category as storage-class specifiers (like static or auto).

    Note that you can just declare:

    typedef struct {
        int number;
        int capacity;
        int passengers;
    }flight;
    

    without the tag name if you intend to only use the type identifier flight.

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