gpt4 book ai didi

c - 空指针与其他指针类型的兼容性

转载 作者:太空宇宙 更新时间:2023-11-04 05:47:30 24 4
gpt4 key购买 nike

void* 可以赋值给任何指针变量,这是 C 语言的一个常见特性。在 N1570 中,C11 的标准文档草案,这在 6.3.2.3 Pointers 中指定:

A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

但是6.2.7兼容类型和复合类型

All declarations that refer to the same object or function shall have compatible type; otherwise, the behavior is undefined.

然而,据我所知,该部分没有void* 与其他指针类型兼容。所以:

int x = 5;
int *xp = &x;
void *vp = xp;

预计在传统和 6.3.2.3 中是完全有效的,但在 6.2.7 中似乎是未定义的行为。

我错过了什么?

最佳答案

关键字:所有声明 ...

int x = 5;
int *xp = &x;
void *vp = xp;

这是三个声明,声明了三个独立的对象:xxpvp

你引用的部分的意思是,如果一个文件说

extern int foo;

另一个文件说

extern double *foo;

行为未定义,因为 foo 已被声明两次,类型不同。

关于c - 空指针与其他指针类型的兼容性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57070494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com