C structs don't define types?

前端 未结 10 2199
离开以前
离开以前 2021-01-21 06:54

I\'ve just started learning C with a professional Java background and some (if no too much) C++ knowledge, and I was astonished that this doesn\'t work in C:

str         


        
10条回答
  •  隐瞒了意图╮
    2021-01-21 07:08

    You first need to make Point a type like :

    typedef struct Point {
        int x;
        int y;
    } Point;
    

    This allows you to use Point as a type, or struct Point

    I.e:

    Point *p;
    
    sizeof(struct Point);
    

    Some people prefer to make the name of the structure different from the type being created, such as:

    typedef struct _point {
           ...
    } Point;
    

    You'll run into both. The last example tells me that I should not use struct _point, its not meant to be exposed except as the type Point.

提交回复
热议问题