use*_*265 37 c struct declaration forward
#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;
}
Run Code Online (Sandbox Code Playgroud)
我在这里遗漏了一些东西.请帮我解决这个问题.谢谢.
ste*_*ert 37
试试这个
#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;
}
Run Code Online (Sandbox Code Playgroud)
Mic*_*ael 37
结构(没有typedef)在使用时通常需要(或应该)使用关键字struct.
struct A; // forward declaration
void function( struct A *a ); // using the 'incomplete' type only as pointer
Run Code Online (Sandbox Code Playgroud)
如果你输入你的结构,你可以省略struct关键字.
typedef struct A A; // forward declaration *and* typedef
void function( A *a );
Run Code Online (Sandbox Code Playgroud)
请注意,重用结构名称是合法的
尝试在代码中将前向声明更改为:
typedef struct context context;
Run Code Online (Sandbox Code Playgroud)
添加后缀以指示结构名称和类型名称可能更具可读性:
typedef struct context_s context_t;
Run Code Online (Sandbox Code Playgroud)