views:

771

answers:

5

I had come across the following code:

typedef struct {
        double x;
        double y;
        double z;
} *vector

Is this a valid type definition? The code compiles and runs fine. I was just curious if this is common practice.

+6  A: 

Absolutely valid. Usually, you can take full advantage of this way by defining two types together:

typedef struct
{
 int a;
 int b;
} S1, *S1PTR;

Where S1 is a struct and S1PTR is the pointer to this struct.

AlexKR
Thanks a lot that makes perfect sense.
Jason
A: 

yes...saves you the trouble of constantly typing the word 'struct' everytime you declare the vector struct or a pointer to it.

Toad
I think the question had to do more with the typedef'ing to *vector rather than just vector.
Andrew Song
A: 

It a valid one, what it does is it defines a new type. As @Alex said, it would be useful to define a type and pointer type.

You could create more pointers just by using

S1PTR ptr1, ptr2, ptr3, ...;

instead of

S1 *ptr1, *ptr2, *ptr3, ...;
Siva
A: 

Yes it is valid. If you need more "security" you can also do

typedef struct vector_{
        double x;
        double y;
        double z;
} *vector;

then you can use both

struct vector_ *var;
vector var;

But don't forget the ending semi-colon.

Using only typedef means that you name it that way. otherwise it'd be more or less anonymous.

Tobias Wärre
+1  A: 

Yes it is valid as described in above answers. A small suggestion, it would be more better if you are providing a tag name too as follows. This would help some of IDE's to parse your code for intellisense.

typedef struct vactor_tag {
        double x;
        double y;
        double z;
} *vector;
FIFO