1

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

Rohan
  • 1,312
  • 3
  • 17
  • 43

3 Answers3

4

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.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
3

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;
WileTheCoyot
  • 513
  • 1
  • 5
  • 20
0

A possible way is as follows:

typedef struct {
        char* name;
        int age;
    } Person;
avmohan
  • 1,820
  • 3
  • 20
  • 39