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.
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.
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.
yes...saves you the trouble of constantly typing the word 'struct' everytime you declare the vector struct or a pointer to it.
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, ...;
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.
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;