c 中的循环声明

发布于 2024-09-12 00:31:56 字数 219 浏览 5 评论 0原文

typedef void (callback)(int *p1, sStruct *p2);

typedef struct _sStruct
{
callback *funct;
}sStruct;

我有以下 C 语言声明。如何编译此循环声明而不收到任何错误?

目前我收到:第一行'*'标记之前的语法错误

typedef void (callback)(int *p1, sStruct *p2);

typedef struct _sStruct
{
callback *funct;
}sStruct;

I have the following declaration, in C. How can I compile this recurrent declaration without receiving any error ?

For the moment I receive: syntax error before '*' token on first line.

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

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

发布评论

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

评论(1

嘦怹 2024-09-19 00:31:56

您可以前向声明结构:

/* Tell the compiler that there will be a struct called _sStruct */
struct _sStruct;

/* Use the full name "struct _sStruct" instead of the typedef'ed name
   "sStruct", since the typedef hasn't occurred yet */
typedef void (callback)(int *p1, struct _sStruct *p2);

/* Now actually define and typedef the structure */
typedef struct _sStruct
{
  callback *funct;
} sStruct;

编辑:更新以匹配问题类型名称的更改。

另外,我强烈建议您不要为该结构指定标识符 _sStruct。以 _ 开头的全局名称是保留名称,将它们用作您自己的标识符可能会导致未定义的行为。

You can forward-declare the structure:

/* Tell the compiler that there will be a struct called _sStruct */
struct _sStruct;

/* Use the full name "struct _sStruct" instead of the typedef'ed name
   "sStruct", since the typedef hasn't occurred yet */
typedef void (callback)(int *p1, struct _sStruct *p2);

/* Now actually define and typedef the structure */
typedef struct _sStruct
{
  callback *funct;
} sStruct;

Edit: Updated to match the question's change of type names.

Also, I strongly suggest that you do not give the struct the identifier _sStruct. Global names beginning with a _ are reserved names, and using them for your own identifiers could cause undefined behavior.

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