I want to know if these two are equivalent and if so, if one is more efficient:
typedef struct test test;
struct test {
bigThing* thing;
anotherThing* thingTwo;
int var1;
int var2;
};
and
typedef struct test {
bigThing* thing;
anotherThing* thingTwo;
int var1;
int var2;
} test ;
CodePudding user response:
both of them are equivalent due to the keyword typedef
as in first example :
typedef struct test test;
struct test {
bigThing* thing;
anotherThing* thingTwo;
int var1;
int var2;
};
when you said typedef struct test test;
this means aliasing the type test
to struct test
which means that wherever you write test
, it's same as writing struct test
.
but look in the second example you provided:
typedef struct test {
bigThing* thing;
anotherThing* thingTwo;
int var1;
int var2;
} test ;
due to the keyword typedef
, meaning that wherever you write the type test
, it's aliased to be
struct test {
bigThing* thing;
anotherThing* thingTwo;
int var1;
int var2;
}
which is the same but it's not convenient to write it like this , if you don't understand the above example , examine the following code :
#include <stdio.h>
int main()
{
struct test{
int x;
int y;
}t1 ;
t1.x = 10;
t1.y = 2;
printf("t1 = {%d, %d}", t1.x, t1.y);
return 0;
}
and here is image of output :
[![enter image description here][1]][1]
so in the first code example you write , writing test x;
is equivalent to :
struct test x;
while in the second code you provided , writing test x;
is equivalent to :
struct test{
int x;
int y;
}x;
where both of them are equivalent but for me , it's more convenient to use the declaration of struct
as in the first example you provided.
[1]: https://i.stack.imgur.com/2pA9P.png
CodePudding user response:
Both your examples will compile to have the same effect. However, the duplication may lead to coders mixing, for instance:
test foobar1;
struct test foobar2;
There is an advantage to using your second example (slightly modified below.)
typedef struct {
bigThing* thing;
anotherThing* thingTwo;
int var1;
int var2;
} test_t;
This version omits the unnecessary token ahead of the curly brace. It also requires one fewer lines of code to be read and understood. Finally, this version uses a convention of "_t" to indicate that it is a (user defined) datatype.
The exception to one claim above is, for instance, declaring a struct to support a linked list. This will need a pointer to instances of itself. Under these requirements, a token must appear before the curly brace:
typedef struct node {
int value;
struct node *next; // "node_t" not encountered yet, so "struct node" required
} node_t;
CodePudding user response:
Is there a noticeable difference between defining & declaring the struct in one block vs. declaring then defining the struct?
No.
I want to know if these two are equivalent
Yes.
if so, if one is more efficient:
There is no difference in program execution. These all are abstract C language things, with no effect on program performance.
The first one is more efficient to type, two words less.