C中的typedef:结构或函数参考? [英] typedef in c: struct or function reference?

查看:67
本文介绍了C中的typedef:结构或函数参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

分析其他人编写的软件的某些部分,发现以下代码行:

Analyzing some part of software written by someone else, I found the following line of code:

typedef const struct rpc_method *(*super_t)(RPC*);

好吧,我知道,

typedef rpc_method *(*super_t)(RPC*);

声明类型 super_t 指针...
我也知道 typedef struct 是什么意思,但是两者的结合?
它是具有单个条目的结构吗???
在这种情况下, const 是什么意思?
是否可以交换 const struct ???
不过我的gcc eabi似乎可以编译了。

declares a type super_t which is a function pointer... I also know what typedef struct means, but the combination of the two?? Is it a struct with a single entry??? And what means const in this context?? Could it be that const and struct are exchanged??? Nevertheless seems to compile with my gcc eabi.

推荐答案

如果声明没有 typedef 将声明变量 IDENTIFIER 作为类型 TYPE 的对象,声明 with typedef 声明 IDENTIFIER 作为类型 TYPE 的类型别名(即相同声明中没有 typedef 的变量具有的类型)。

If a declaration without typedef would declare a variable IDENTIFIER as an object of type TYPE, a declaration with typedef declares IDENTIFIER as type alias for type TYPE (i.e., the same type that the variable in the same declaration without the typedef would have).

因此, >

So given that

struct rpc_method const*(*super_t)(RPC*);

super_t 声明为函数的指针获取指向 RCP 的指针并返回指向 struct rpc_method const

declares super_t as a pointer to a function taking a pointer to RCP and returning a pointer to struct rpc_method const,

typedef struct rpc_method const*(*super_t)(RPC*);

super_t 声明为上面的指针的类型(指向使用 RCP 的指针并返回 struct rpc_method const 的指针的函数)。

declares super_t as a type alias for the type of the above pointer (to a function taking a pointer to RCP and returning a pointer to struct rpc_method const).

以上声明中的 const 限定返回类型的目标,无论您将其写为 struct rpc_method const * const struct rpc_method * (将限定符放在类型的右侧可以使它与限定词放在链接指针的其他链接中,例如,在 struct foo const const * const * volatile bar 中,最后两个 const volatile 必须位于其 * 的右侧,而第一个 const 可以在 struct foo 的左侧保留,而不会更改语义。

The const in the above declaration qualifies the target of the return type, whether you write it as struct rpc_method const* or const struct rpc_method* (putting the qualifer at the right hand side of the type works makes it more consistent with qualifiers put in other linkes of a chained pointer E.g., in struct foo const * const * volatile bar the last two const and volatile have to be at the right hand side of their *, while the first const could go left of struct foo with no change of semantics.

更多详细信息:

https://stackoverflow.co m / a / 57793519/1084774

这篇关于C中的typedef:结构或函数参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆