【发布时间】:2015-10-30 18:42:09
【问题描述】:
#include<iostream>
using namespace std;
struct a{
int e;
struct abc *d;
};
struct abc{
int c;
};
int main()
{
return 0;
}
我在定义struct a 之后定义了struct abc,我在其中声明了abc 的结构指针。这应该会引发编译错误,因为在声明之前使用了abc。但是,它没有,为什么?
而当我将其替换为 struct abc d 而不是 struct abc *d 时,它会按预期给出编译错误。
【问题讨论】:
-
拥有一个指向未知数据类型的指针不需要额外的信息。- 指针就是指针。另一方面,无法解析未知数据类型的实例,因为您需要知道结构的大小才能为其分配空间。
-
不,没有使用。编译器知道指向结构的 指针 的大小,因为这些指针的大小始终相同。
-
研究不足的问题。
-
@BoPersson 但有些架构
sizeof(char*) != sizeof(int*)Can the size of pointers vary between data and function pointers? Are there are any platforms where pointers to different types have different sizes? 在这些情况下编译器如何知道指针大小? -
@LưuVĩnhPhúc - 是的,但指向结构和类的指针必须在给定系统上都具有相同的大小。
标签: c++ pointers struct structure