문제

I have the following code below.

typedef struct person Person;
    Person {
        char* name;
        int age;
    };

From what I understand, typedef will substitute "struct person" with Person. So when making the struct, it is equal to writing :

    struct person {
        char* name;
        int age;
    };

Is this thinking correct? Because I am getting an error an error of the first line of the struct.

error: expected identifier or ‘(’ before ‘{’ token This error is referring to the line : Person {

Any help is appreciated. Thanks

도움이 되었습니까?

해결책

Do it like this

typedef struct person Person;
struct person  {
        char* name;
        int age;
};

Then you can use Person for all usages of the struct.

also there is no need in different capitalization

typedef struct person person;

would do equally well.

다른 팁

One way to do what you want is:

struct person {
    char* name;
    int age;
};
typedef struct person Person;

Or, if you want to accomplish this in one instruction, you could do:

typedef struct person {
    char* name;
    int age;
} Person;

A possible way is as follows:

typedef struct {
        char* name;
        int age;
    } Person;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top