C 中的函数指针和指针参数

发布于 2024-11-02 21:39:47 字数 985 浏览 3 评论 0原文

对于 C 来说,完全是菜鸟,刚刚开始闲逛,想知道如何(阅读“if”)下面的内容是可能的。

尝试创建一个struct,其成员是函数指针,并且该函数指针指向一个函数,该函数采用与上述struct相同类型的参数。例如(注意语法,只是在这里熟悉一下):

typedef struct{
    void (*myStructFunc)(void);
} MyStructType;

void myFunc(void){
    printf("Hello world!");
}

// ...

MyStructType myStruct;
myStruct.myStructFunc = &myFunc;
myStruct.myStructFunc(); // <= Hello world!

This 工作正常,但是当我尝试向函数引入 MyStructType 类型的参数时:

typedef struct{
    void (*myStructFunc)(*MyStructType); // <= parse error
} MyStructType;

void myFunc(MyStructType *myStruct){
    printf("Hello world!");
}

// ...

MyStructType myStruct;
myStruct.myStructFunc = &myFunc;
myStruct.myStructFunc(&myStruct);

这些示例很简短原因显而易见,但它们说明了我的意图。再说一遍,我刚刚接触了C,所以请原谅任何语法上的无知。

无论如何,我怎样才能实现这一目标?显然我在语法上做了一些不正确的事情,或者也许我正在尝试做一些明显不可能的事情。

另请注意,这样做的原因纯粹是学术性的。

Complete noob to C, just getting started with some goofing around, wondering how (read "if") the following is possible.

Trying to create a struct, with a member that is a function pointer, and the function pointer points to a function that takes an argument with the same type as the aforementioned struct. For example (mind the syntax, just getting familiar here):

typedef struct{
    void (*myStructFunc)(void);
} MyStructType;

void myFunc(void){
    printf("Hello world!");
}

// ...

MyStructType myStruct;
myStruct.myStructFunc = &myFunc;
myStruct.myStructFunc(); // <= Hello world!

This works fine, but when I try to introduce arguments of the MyStructType type to the function:

typedef struct{
    void (*myStructFunc)(*MyStructType); // <= parse error
} MyStructType;

void myFunc(MyStructType *myStruct){
    printf("Hello world!");
}

// ...

MyStructType myStruct;
myStruct.myStructFunc = &myFunc;
myStruct.myStructFunc(&myStruct);

These examples are brief for obvious reasons, but they illustrate my intentions. Again, just getting my feet wet with C so please forgive any syntactical ignorance.

Anyways, how can I achieve this? Clearly I'm doing something incorrect syntactically, or perhaps I'm trying to do something that's plain not possible.

Also note, that the reason for this is purely academic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

百变从容 2024-11-09 21:39:47

在第二个示例中,在声明函数时未定义名称 MyStructType 。另外,您的 * 位于错误的位置。您需要执行类似于以下操作的操作:

typedef struct MyStruct {
    void (*myStructFunc)(struct MyStruct *);
} MyStructType;

In your second example, the name MyStructType isn't defined when you're declaring your function. Also, you have the * in the wrong spot. You need to do something similar to the following:

typedef struct MyStruct {
    void (*myStructFunc)(struct MyStruct *);
} MyStructType;
风轻花落早 2024-11-09 21:39:47

解析错误来自 *MyStructType,这似乎是一个拼写错误,因为您在实际函数定义中正确声明了结构指针。

修复该问题仍然是一个错误,因为在定义 struct 时,typedef 尚未生效。同样的问题可以用链表来说明(更清楚地恕我直言):

typedef struct {
  node *next; // error - what is a node?
  void *data;
} node; // this is where node is defined

要解决它,您需要使用命名的struct

struct node {
  struct node *next;
  void *data;
};

或者先放置typedef,然后定义(再次命名)struct

typedef struct node node;

struct node {
  node *next;
  void *data;
};

或者(如果您感觉令人讨厌)只需使用 void * 指针:

typedef struct {
  void *next;
  void *data;
} node;

这同样适用于您的情况。

The parse error is coming from *MyStructType, which appears to be a typo as you've correctly declared the struct pointer in the actual function definition.

Fixing that is still an error because, at the time of the struct definition, the typedef hasn't taken effect. The same problem can be illustrated (more clearly IMHO) with a linked list:

typedef struct {
  node *next; // error - what is a node?
  void *data;
} node; // this is where node is defined

To solve it, you need to use a named struct:

struct node {
  struct node *next;
  void *data;
};

Or put the typedef first, then define the (again named) struct:

typedef struct node node;

struct node {
  node *next;
  void *data;
};

Or (if you're feeling obnoxious) just use a void * pointer:

typedef struct {
  void *next;
  void *data;
} node;

The same applies to your situation.

你与昨日 2024-11-09 21:39:47

尝试:

struct MyStructType {
  void (*myStructFunc)(struct MyStructType*);
};

相反。在 C 中,结构体位于自己的命名空间中,因此如果要引用结构体,则需要在名称前添加 struct 前缀。您的 typedef 在您引用之后才可见,这是一个错误。如果你仍然想要 typedef,你可以这样做:

typedef MyStructType MyStructType_t;

Try:

struct MyStructType {
  void (*myStructFunc)(struct MyStructType*);
};

instead. In C, structs are in their own namespace, so if you want to refer to a struct, you need to prefix the name by struct. Your typedef isn't seen until after you reference, which is an error. If you still want the typedef, you can do:

typedef MyStructType MyStructType_t;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文