Reputation: 893
If i want to define a struct in c i write
typedef struct ObjectP
{
some code
}
and this is legal, but what about this code line ?
typedef struct Object* ObjectP
{
some code
}
why there is a syntax error on the line typedef struct Object* ObjectP
Upvotes: 0
Views: 133
Reputation: 6116
You are using wrong syntax for typedef
ing the struct
pointer.
Use:
typedef struct Object
{
some code
}
*ObjectP;
Now you can use ObjectP
to define pointers to this struct
this way: ObjectP ptr1_struct, ptr2_struct;
.
Upvotes: 2
Reputation: 2171
Structure definition should end with ";". typedef is not part of structure definition. It is a renaming convention.
Upvotes: 1
Reputation: 123458
The syntax for a struct definition is
struct identifieropt { struct-declaration-list }
The optional identifier is the struct tag; it's the name by which the struct type can be referenced. For example:
struct foo { int x; int y; double z; };
foo
is the tag for that struct type; I can then use struct foo
anywhere I need a type name:
struct foo bar;
struct foo *fptr;
I can combine the struct definition and object declaration in one:
struct foo { int x; int y; double z; } bar, *fptr;
If I want to create a typedef name for a pointer to struct foo
, I do it as follows:
typedef struct foo { int x; int y; double z} *FooP;
FooP
is now a synonym for struct foo *
:
FooP myptr;
is equivalent to
struct foo { int x; int y; double z; } *myptr;
Upvotes: 0
Reputation: 409136
That's not how you use typedef
with structures. You have to put the type-alias name after the structure, like
typedef struct Object
{
...
} Object;
And if you want multiple type-aliases, you just add them to then end, as in
typedef struct Object
{
...
} Object, *ObjectP;
Or you separate the definitions:
struct Object
{
...
};
typedef struct Object Object;
typedef struct Object *ObjectP;
Upvotes: 5