C typedef of pointer to structure

user187174 picture user187174 · Oct 9, 2009 · Viewed 55.8k times · Source

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.

Answer

alexkr picture alexkr · Oct 9, 2009

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.