Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top