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;
Related
I have a struct which currently looks like this (abbreviated to show only the essential parts):
typedef struct {
uint32_t baudrate;
... some other internally used values here
void (*request_received)(void* hbus); //< this is what I'm talking about
} hbus_options_t;
This works. Basically it contains a function pointer which takes a pointer to a parameter of type void.
What I would actually like is for this to be easier to understand:
typedef struct {
uint32_t baudrate;
... some other internally used values here
void (*request_received)(hbus_options_t* hbus); //< this doesn't work
} hbus_options_t;
Obviously the compiler needs to know the struct before I can use it. How is this done usually? Using a void pointer works but it's harder to understand.
It's done by not being remiss and providing a struct tag:
typedef struct hbus_options {
uint32_t baudrate;
... some other internally used values here
void (*request_received)(struct hbus_options * hbus);
} hbus_options_t;
Besides readability, the compiler will also complain if you pass a pointer to something other than the intended struct type.
Adding a tag also allows for looser coupling of components. One can forward declare a structure, but not a type alias.
How can I fix this warning?
typedef void (*VF_A)(PST_A); // Warning here: parameter names (without types) in function declaration
typedef struct ST_A_ {
...
VF_A vf_a;
} ST_A, *PST_A;
This question is similar to Resolve circular typedef dependency?, but yours is slightly different in that you have a pointer to a function instead of a struct. Use the strategy from this answer.
The idea behind the problem is that you are trying to declare a new type and also define a struct at the same time. The solution is to separate these two:
typedef struct ST_A_ ST_A, *PST_A; // PST_A points to some struct, defined later
typedef void (*VF_A)(PST_A); // use PST_A to define VF_A
struct ST_A_ { VF_A vf_a; }; // now define the struct PST_A points to
Tongue twister title, but I'm really probably just overthinking it.
I have a struct that has a callback function pointer in it that takes as input parameter a type that is that struct. How to go about it?
typedef struct my_type my_type;
void (*my_callback_f)(my_type *Type);
struct my_type
{
my_callback_f my_callback;
};
The compiler obviously does not like this but hopefully it illustrates what I'm after. Like I said, I'm probably just overthinking context pointers.
Edit (Instantiating):
my_type *TheThing = { MyFunction };
where
void MyFunction (my_type *Type)
{
//stuff
}
This results in the function address in the object's function pointer not matching the known address for MyFunction.
Worst case, just forward declare the struct;
struct my_type; // this is the forward declaration
typedef void (*my_callback_f)(struct my_type *Type);
struct my_type
{
my_callback_f my_callback;
};
I'm going to "answer" my own question simply because I solved my issue, but I'll accept Soren's as the answer since his answer technically answers the subject question I asked originally.
The function pointers were throwing illegal access errors because I had not malloc'ed the struct that I had instantiated. Once I did that it called the function just fine, despite having different values according to the debugger.
Isn't forward declaration, whether for structures or functions, supposed to do what forward declaration is expected to do, ie, to let us use the structure or function before they are defined? Why is the forward declaration of a structure not working in my code? And the main thing that just misses me, is forward declaration of structures of any use in C at all? When is it used? Can you please give me a small C program example to illustrate this?
My program gives the error error: storage size of 'x' isn't known|.
#include<stdio.h>
struct test;
int main(void)
{
struct test x;
printf("%zu",sizeof(x)); //Gives Error
//printf("%zu",sizeof(struct test));//This fails too
}
struct test
{
int a;
char b;
};
New Edit I tried to do what Carl Noum said,but even this is not working:
#include<stdio.h>
struct test;
void foo(struct test*);
int main(void)
{
struct test x={53,'x'},*ptr=&x;
foo(ptr);
}
void foo(struct test* p)
{
printf("%d,%c",p->a,p->b);
}
struct test
{
int a;
char b;
};
The compiler has to know the struct's layout when it compiles the main function.
A forward declaration is useful if you only have a pointer but not the actual type.
For example if you have a struct that contains a pointer to another struct
struct foo {
struct bar *b;
...
};
It is also essential if the bar also contain foo like
struct bar;
struct foo {
struct bar *b;
};
struct bar {
struct foo f;
};
In this case you have to have bar pre-declared.
A forward declaration usually means that you don't have to include .h file inside other .h file. This can speed up compilation significantly if the .h file is big.
Functions yes, structures no. struct test is an incomplete type where you use it.
A common use case for incomplete types is to declare an opaque type. In a header file, you declare:
struct test;
And then some API that uses struct test only via pointers:
int func1(struct test *);
struct test *func2(void);
In the accompanying implementation, you include the full declaration so that your functions know what to do with the structure:
struct test
{
int a;
char b;
};
void func1(struct test *t)
{
return t->a;
}
Edit:
Your new code doesn't do anything differently - you're still trying to operate on an incomplete type, and you still can't do that. In particular, this declaration:
struct test x = {53,'x'};
Can't work if struct test is an incomplete type. You can (generally) only use pointers to an incomplete type. In this case, that might mean creating a function that allocates and returns a pointer to a new structure, rather than trying to declare and initialize one on the stack.
Struct type declared by a forward declaration (i.e. an incomplete type) can be used only in a limited number of ways. Applying sizeof to such a truct type is not one of them. On top of that, you can't use incomplete types in object definitions and you cannot access data fields of incomplete struct types.
In other words, sizeof requires a complete type. Your forward-declared struct type is not a complete type. Operator -> also requres a complete type of the left-hand side. Object definition (like struct test x) also requires a complete type.
I've got following code fragment and I want to know if it is pure C or it contains some C++ elements. This question stems from the fact that I think it is only C, but some compilers don't accept the code.
// User struct derived from FunctionBlock
struct Function{
// Inputs
int codeGenerationIterator;
int i;
char* s;
// Outputs
// Internal
};
void FunctionCall(struct Function *arg){
}
void FunctionConstructor(struct Function *arg){
arg->i=3;
arg->s="!";
// Call constructor for all not primitive variables
// Create struct with first call
FunctionCall(arg);
}
// User type definition
typedef struct Punto{
int codeGenerationIterator;
Function x[3+1];
Function *x_pointer[3+1];
double y;
};
void PuntoConstructor(struct Punto *arg){
// Call constructor for all not primitive variables
for(arg->codeGenerationIterator=0;arg->codeGenerationIterator<=3;arg->codeGenerationIterator++){
arg->x_pointer[arg->codeGenerationIterator]=&(arg->x[arg->codeGenerationIterator]);
FunctionConstructor(arg->x_pointer[arg->codeGenerationIterator]);
}
}
// User type definition
typedef struct Cerchio{
int codeGenerationIterator;
double r;
Punto centro;
Punto *centro_pointer;
};
void CerchioConstructor(struct Cerchio *arg){
// Call constructor for all not primitive variables
arg->centro_pointer=&(arg->centro);
PuntoConstructor(arg->centro_pointer);
}
// User type definition
typedef struct Container{
int codeGenerationIterator;
Cerchio circonferenza[10+1];
Cerchio *circonferenza_pointer[10+1];
};
void ContainerConstructor(struct Container *arg){
// Call constructor for all not primitive variables
for(arg->codeGenerationIterator=0;arg->codeGenerationIterator<=10;arg->codeGenerationIterator++){
arg->circonferenza_pointer[arg->codeGenerationIterator]=&(arg->circonferenza[arg->codeGenerationIterator]);
CerchioConstructor(arg->circonferenza_pointer[arg->codeGenerationIterator]);
}
}
int main(void){
// Variable definitions
int codeGenerationIterator;
int count;
Punto insiemePunti[50+1];
Punto *insiemePunti_pointer[50+1];
Cerchio cerchio;
Cerchio *cerchio_pointer;
Container container;
Container *container_pointer;
Container containers[11+1];
Container *containers_pointer[11+1];
// Call constructor for all not primitive variables
for(codeGenerationIterator=0;codeGenerationIterator<=50;codeGenerationIterator++){
insiemePunti_pointer[codeGenerationIterator]=&insiemePunti[codeGenerationIterator];
PuntoConstructor(insiemePunti_pointer[codeGenerationIterator]);
}
cerchio_pointer=&cerchio;
CerchioConstructor(cerchio_pointer);
container_pointer=&container;
ContainerConstructor(container_pointer);
for(codeGenerationIterator=0;codeGenerationIterator<=11;codeGenerationIterator++){
containers_pointer[codeGenerationIterator]=&containers[codeGenerationIterator];
ContainerConstructor(containers_pointer[codeGenerationIterator]);
}
container.circonferenza[1].centro.x[0].i=2;
containers[2].circonferenza[2].centro.x[4].i=2;
printf("Works!");
getchar();getchar(); // TODO: delete
return 0;
}
As you can see I haven't use classes or overloading but only simple instructions, structs and some pointers. So, why do some strict C compilers give me an error?
No, this is not valid C. This line:
Function x[3+1];
lacks the struct keyword, and there's no typedef struct Function Function; to introduce the type alias you seem to be using.
Also, the use of // comments requires a recent-enough compiler, that syntax wasn't added to C officially until C99. An older compiler would fail for this reason, too.
Some example of compilers are: "Miracle C", "Tiny C" and the error does not provide any information
Maybe try a real compiler then? How about just take your code and throw it in GCC to get better error messages.
Despite that fact, it sounds like you are getting error messages: "missing ';' at Probably just means something to the compiler looks like it should have been two statements and you forgot to break it up with a ;, it might not be the best error message, but no compiler can always tell you outright what's wrong, they're just hints becuase they expect you to be smarter. :)
You have a bunch of structs in your code:
typedef struct Cerchio {
typedef struct Punto {
struct Function{
One of these structs is not like the others.
If you use the typedef you can refer to the structure just by the name Cerchio (for example).
If you do not use typedef you need to explicitly call it everytime: struct Function
In addition to the missing struct keywords, your code must #include <stdio.h>. In C99 this is required in order to be a well-formed program. In C89 the program is well-formed without it (once the other errors are fixed) but has undefined behavior.
gcc -pedantic-errors -std=c99 will give you the exact line numbers that contain errors, and in addition gives some warnings that you should pay attention to. Your definitions like:
typedef struct Punto { ... };
are all wrong even though they're legal. The typedef has no effect since you don't provide a name for the type you're supposedly def-ing. So the only way to identify it in C is as struct Punto, not Punto as you'd like and as it can be identified in C++.