I'm a beginner in C and I came across some codes in a header file that I'm not sure what style it uses and what it means.
typedef struct tActiveObject *testActiveObjectPtr;
typedef struct tActiveObject {
ActiveObject ao;
int state;
} testActiveObject, *testActiveObjectPtr;
Why do we need to create a pointer as an alias i.e. testActiveObject and *testActiveObjectPtr? And is this just some C style that I am not aware of?
Thanks.
If the both those typedefs occur in the same header file, then the code doesn't make any sense. In that case, the first typedef is completely superfluous and the whole code could get replaced with
typedef struct {
ActiveObject ao;
int state;
} testActiveObject, *testActiveObjectPtr;
Otherwise if the typedefs were in different files, the code might have been a failed attempt to create a pointer to an incomplete type, but it doesn't look like that's the case. The struct tag is superfluous, but also smells like a failed attempt to create a self-referencing struct.
Also, good programming practice dictates that you never hide a pointer behind a typedef.
So it would seem the whole code was created by a rather confused person who didn't quite know what they were doing. If possible, throw the code away and replace it with:
typedef struct {
ActiveObject ao;
int state;
} testActiveObject,
...
testActiveObject* ptr; // whenever you need a pointer to this type
Related
Suppose we have a function pointer on a struct, which has the struct itself as the first argument, a typical callback scenario.
typedef void (*callback_type)(my_struct_type *mst, int whatever);
typedef struct {
// lots of fun stuff
callback_type notify_me;
} my_struct_type;
This produces a compiler error on the first typedef, as one might expect. error: unknown type name my_struct_type. Reversing the definitions produces the same result, but the unknown type is callback_type.
The easy solution is to do the following:
typedef struct my_struct_type_S {
// lots of fun stuff
void (*notify_me)(my_struct_type_S *mst, int whatever);
} my_struct_type;
However, doing this elides the function pointer type definition, which it would be nice to be able to easily refer to later, and use for static type checks, nice error messages, etc.
Any suggestions on how to resolve this?
Edit on "possible duplicate":
This scenario involves function pointer typedefs that are arcane to many people. I think this is a good example for that case, and additionally, the accepted answer is very clean, clear, and simple.
You can do this by giving the struct a tag and using a forward declaration of the struct. Then you can use the typedef for the function pointer, and subsequently complete the definition of the struct.
typedef struct my_struct_type_S my_struct_type;
typedef void (*callback_type)(my_struct_type *mst, int whatever);
struct my_struct_type_S {
// lots of fun stuff
callback_type notify_me;
};
You need to define the tag of the struct
typedef void (*callback_type)(struct _my_struct_type *mst, int whatever);
typedef struct _my_struct_type {
// lots of fun stuff
callback_type notify_me;
} my_struct_type;
How can I use struct A to modify the data inside a struct B. Which has no name, just a type.
struct A {
struct B;
};
struct B {
int data;
};
Since this is for school, I can't change the code above. I can only use it. I tried something like this for my main but it doesn't work
int main (){
struct A myStruct;
myStruct.B.data = 3;
return 0;
}
Thanks in advance.
Edit: Sorry I was just trying to post this as fast as possible that's why I didn't post this with proper c syntax. Anyway, it's my fault for not being clear enough on my question.
I'm aware that my main doesn't work I just want to know if it's ever possible to access the data inside struct B without declaring a name for it inside struct A as I have above. This is the code I was given by a teacher, so I didn't want to modify the structs because I thought maybe she wants us to brainstorm a way to use it the way she wrote it.
The way iharob explains it works perfectly by declaring struct B before struct A, and actually giving a name to struct B.
Is it simply not possible to access that data inside struct B without giving it a name?
The code you posted is not even c code, it would not compile.
Your main mistake is that you don't need to use the struct name to access the member. This should be good
struct B
{
int data;
};
struct A
{
struct B member;
};
int main(void)
{
struct A instance;
instance.member.data = 3;
return 0;
}
I assume that you posted some sample code, don't do that. Post the actual code that has issues. The code you posted is completely invalid because some one of the definitions lack the type, you can't declare structs without using struct in c except if you typedef it. So please post the actual code the next time.
And don't build such complicated structs with struct members unless you really know what you are doing.
This question already has answers here:
Understanding typedef with struct [duplicate]
(6 answers)
Closed 5 years ago.
I have few questions connected with struct and typedef, there is piece of code and I have marked some places where I'm not sure if the syntax is correct. I use Eclipse editor and it shows me when there is problem in compilation. I just don't understand why is sometimes keyword struct needed and sometimes not. I may have also some mistakes of using this keyword. So plese help me to understand it.
let's have struct
typedef struct player
{
char *name;
int gameId;
int points;
int socketfd; //socket descriptor of player
int state;
} player_struct;
lets have another struct
#define PLAYERSLEN 2
typedef struct game{
struct player_struct *players[PLAYERSLEN]; //PLACE1
//some code
} game_struct;
let's have function
player_struct *create_player() //PLACE2
{
player_struct *player; //PLACE3
//alokace pameti
player = (player_struct *) malloc(sizeof(player_struct)); //PLACE4
//PLACE5
player->gameId = -1;
player->points = 0;
player->state = 0;
return player;
}
let's have function? In fact what does this definition mean?
void *( player_struct *player) //PLACE6
{
//some code
}
Questions references:
PLACE1 - is this correct? why can't I use just player_struct *players[PLAYERSLEN]; ??
PLACE2 - it looks like there is not needed struct before player_struct , is it correct? why?
PLACE3 - it looks like there is struct also not needed, is it correct? why?
PLACE4 - it looks like there is struct also not needed, is it correct? why?
PLACE4 & PLACE5 I may should handle errors there, cause there is malloc so I should probably put all the line with PLACE4 to if and if the malloc fails I should put at PLACE 5 free(player). Am I right?
PLACE6 what could have mean this function or whatever it is? The code inside brackets which is not included here should delete the player .. I just don't understand the syntax of wrote function - what does it mean?
PLACE6 - again similar as previous why is not necessary put the keyword struct before player_struct at this line? is it correct?
Really thanks you for your time.
You're doing two things in the initial definition:
You're defining the structure with the type name struct player.
You're creating a typedef called player_struct, which is just an alias for struct player.
As such, either struct player or player_struct is correct (and more-or-less equivalent), but struct player_struct refers to a completely separate type, and is probably incorrect unless you're trying to confuse people.
Now, handling your six cases in order:
PLACE1: As noted above, this code is actually wrong as it stands.
PLACE2, PLACE3, PLACE4: All correct; see above for an explanation.
PLACE5: Yes, you should probably check that player != NULL here just to be sure.
PLACE6: Again, this is correct, and the reason why is above. I'm not sure what void *(...) is supposed to be, though -- if * is actually a function name then it's probably fine though.
It's because the labels are in two separate namespaces (yes, even in C). There's a special namespace for structs.
I like to declare the label in both namespaces, using this:
typedef struct mon_struct {
int a;
} mon_struct;
then you can use either/both struct mon_struct mon = ... or just mon_struct mon = ....
Update
If you want to use just one label, then you can use one of the following:
struct mon_struct { int a; };
// requires namespace resolution using struct tag:
void f(struct mon_struct p);
// -= OR =-
// slightly awkward declaration
typedef struct { int a; } mon_struct;
// but the type exists in the global namespace,
// so we don't need to use the struct tag:
void f(mon_struct p);
but this becomes messy if you are using the struct in both C and C++, particularly when the declaration or implementations move between C and C++ translations without proper guarding (extern "C" { ...stuff... }).
so you might opt to declare it in both namespaces to minimise breakage:
typedef struct mon_struct { int a; } mon_struct;
void f(struct mon_struct p); // << ok
void f2(mon_struct p); // << ok
No. player_struct is a typedef for struct player. You don't need the struct keyword in this case.
Correct. You created a type definition called player_struct which means struct player.
Same reason as 2.
Same reason as 2 & 3.
Yes, you should check to see if malloc() returns NULL. In addition, don't cast the return value from malloc(). Doing so can hide #include errors.
That's not a valid function definition. You don't need struct because of the typedef - same as above.
You may find this all makes more sense if you remove the words typedef, player_struct, and game_struct from your code entirely. Then once you get used to how that all works, you can reintroduce the typedefs and maybe cut down on some typing. As a quick example, you can break down your first definition into its components:
struct player
{
char *name;
int gameId;
int points;
int socketfd; //socket descriptor of player
int state;
};
typedef struct player player_struct;
Maybe that will help you make sense of it?
You don't need struct in PLACE1... as long the definition of player_struct is known, ie, declared before it, or from a .h before it.
I’m trying to refactor and bring-over some old code, and I ran across something like this:
struct foo;
typedef struct foo * foo;
When attempting to compile it, I get the following sort of error:
Source/Types/Types.h:27:18: error: redefinition of 'foo' as different kind of symbol
typedef struct foo * foo;
^
Anybody know what’s causing this? I haven’t touched the code in a long time, but I certainly don’t remember any errors relating to that. It’s some of the most core code in the codebase, everything depends on it; I don’t see how I could possibly have missed such a glaring error, if it is indeed an error. Since the original foo is a “struct tag” (only a sane reference after the struct keyword), how can it conflict with my new foo typedef’d type?
Edit 1: Here’s the entire actual file, maybe I’m missing something, but it seems pretty straight-forward. It dumps a slew of the same errors described above, one for each type:
# if !defined(TYPE_DECLARATIONS)
# define TYPE_DECLARATIONS
# include "Core.h"
# warning "in Core.h"
static int class = 0; // to prove I’m not compiling as C++
struct e(fork);
typedef struct e(fork)* e(fork);
struct e(execution);
typedef struct e(execution)* e(execution);
struct e(thing);
typedef struct e(thing) e(thing);
struct e(typeRepresentation);
typedef struct e(typeRepresentation)* e(typeRepresentation);
struct e(typeRepresentation) {
void * family;
char name[64]; };
struct e(thing) {
void * const pointer;
e(typeRepresentation) const isa; };
# endif //!defined(TYPE_DECLARATIONS)
(Also, ignore the e() macro; it’s a noop in this case.)
Ah, I solved my own problem. I managed to screw up my e() macro such that it was literally preforming a noop, removing the code in question. :O
I’m an idiot. Sorry for wasting everyone’s time.
Are you now compiling this as C++, whereas it used to be compiled as C?
In C++, struct and enum tags live in the same namespace as other type names.
you associate the name foo with two different types. struct foo and a pointer to struct foo are different types.
Sometimes I see code like this (I hope I remember it correctly):
typedef struct st {
int a; char b;
} *stp;
While the usual pattern that I familiar with, is:
typedef struct st {
int a; char b;
} st;
So what's the advantage in the first code example?
You probably mean this:
typedef struct ST {
/* fields omitted */
} *STP;
The asterisk is at the end of the statement. This simply means "define the type STP to be a pointer to a struct of this type". The struct tag (ST) is not needed, it's only useful if you want to be able to refer to the struct type by itself, later on.
You could also have both, like so:
typedef struct {
/* fields omitted */
} ST, *STP;
This would make it possible to use ST to refer to the struct type itself, and STP for pointers to ST.
Personally I find it a very bad practice to include the asterisk in typedefs, since it tries to encode something (the fact that the type is a pointer) into the name of the type, when C already provides its own mechanism (the asterisk) to show this. It makes it very confusing and breaks the symmetry of the asterisk, which appears both in declaration and use of pointers.
It's a habit that stems from the time when typedef names and struct tagnames were in the same namespace. See http://blogs.msdn.com/oldnewthing/archive/2008/03/26/8336829.aspx
I think you are talking about :
typedef struct{
int a;
char b;
} object, *objectPointer;
This means that (new) type objectPointer is a pointer to struct (object) defined above. Its easy to declare pointers to object struct this way. For instance,
objectPointer A = (objectPointer)malloc(sizeof(object));
A->a = 2;
Now, A is a pointer to struct object and you can access its variables as described above.
In case, objectPointer was not defined,
struct object *A = (struct object *)malloc(sizeof(object));
A->a = 2;
So, I guess objectPointer is more intuitive and easy to use.
I hope that the first code would say a compiler error ,
I see no good reason for the typedef name be different from the tag name.
Now, the reason for which the tag name needs to be typedefed if you don't want to use
struct tag v;
but
tag v;
is probably an historical one. For as long as I remember, C had typedef but I don't know if it was true when struct have been introduced (handling of typedef is a nuisance in the C grammar). In the old code I've seen, using typedef for struct isn't done, and there are things like unix
struct stat;
int stat(const char*, struct stat*);
which would break with an automatic typedef. One those are introduced, changing is quite difficult (yes, C++ has automatic typedef but C++ has special wording to handle that case of overloading and it would be yet another complication).