1: void f(mystruct *a)
2: void f(const mystruct *a)
从 1->2 更改函数签名会破坏 C 中的 API/ABI 吗?
改变 2->1 怎么样?
最佳答案
来自 C99 标准 6.2.5/26“类型”:
pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements.
因此,从 1 到 2,ABI/API 不应受到影响。(API 不会改变,因为指向非 const 限定类型的指针可能会转换为指向 const 限定版本的指针类型 - 6.3.2.3/2“转换 - 指针”)。
但是,如果从 2 变为 1,则 API 会发生变化,因为指向 const 对象的指针无法隐式转换为指向非 const 对象的指针。以下代码可以在版本 2 下编译,但不能在版本 1 下编译:
static const mystruct foo;
f(&foo);
关于c - 将 f(mystruct *a) 更改为 f(const mystruct *a) 会破坏 C 中的 API/ABI 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5083765/