Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

forward declaration of a struct in C?

#include <stdio.h>  struct context;  struct funcptrs{   void (*func0)(context *ctx);   void (*func1)(void); };  struct context{     funcptrs fps; };   void func1 (void) { printf( "1\n" ); } void func0 (context *ctx) { printf( "0\n" ); }  void getContext(context *con){     con=?; // please fill this with a dummy example so that I can get this working. Thanks. }  int main(int argc, char *argv[]){  funcptrs funcs = { func0, func1 };    context *c;    getContext(c);    c->fps.func0(c);    getchar();    return 0; } 

I am missing something here. Please help me fix this. Thanks.

like image 456
user1128265 Avatar asked Apr 03 '12 18:04

user1128265


People also ask

Can you forward declare a struct?

In C++, classes and structs can be forward-declared like this: class MyClass; struct MyStruct; In C++, classes can be forward-declared if you only need to use the pointer-to-that-class type (since all object pointers are the same size, and this is what the compiler cares about).

What is forward declaration in C?

As others stated before, a forward declaration in C/C++ is the declaration of something with the actual definition unavailable. Its a declaration telling the compiler "there is a data type ABC".

What is forward declaration in Objective C?

Forward declarations are mainly to avoid circular imports, where one file imports another file which imports the first file etc. Basically when you import a file, contents of the file are substituted at the point of import when you build your project, which is then fed to the compiler.

What is forward declaration of a function?

A forward declaration allows us to tell the compiler about the existence of an identifier before actually defining the identifier. In the case of functions, this allows us to tell the compiler about the existence of a function before we define the function's body.


2 Answers

A struct (without a typedef) often needs to (or should) be with the keyword struct when used.

struct A;                      // forward declaration void function( struct A *a );  // using the 'incomplete' type only as pointer 

If you typedef your struct you can leave out the struct keyword.

typedef struct A A;          // forward declaration *and* typedef void function( A *a ); 

Note that it is legal to reuse the struct name

Try changing the forward declaration to this in your code:

typedef struct context context; 

It might be more readable to do add a suffix to indicate struct name and type name:

typedef struct context_s context_t; 
like image 95
Michael Avatar answered Sep 23 '22 03:09

Michael


Try this

#include <stdio.h>  struct context;  struct funcptrs{   void (*func0)(struct context *ctx);   void (*func1)(void); };  struct context{     struct funcptrs fps; };   void func1 (void) { printf( "1\n" ); } void func0 (struct context *ctx) { printf( "0\n" ); }  void getContext(struct context *con){     con->fps.func0 = func0;       con->fps.func1 = func1;   }  int main(int argc, char *argv[]){  struct context c;    c.fps.func0 = func0;    c.fps.func1 = func1;    getContext(&c);    c.fps.func0(&c);    getchar();    return 0; } 
like image 43
stefan bachert Avatar answered Sep 22 '22 03:09

stefan bachert