为什么只有指针变量才能保存任何其他变量的地址?如果我们将任何变量的地址存储在简单变量中,那么它将将该地址存储为值。指针变量设计成什么样的数据结构?
最佳答案
我相信您忽略了一点:指针也是一种类型,一种单独的类型,就像标准整数类型、浮点类型等一样。
引用 C11
,第 §6.2.5 章
- A pointer type may be derived from a function type or an object type, called the referenced type. A pointer type describes an object whose value provides a reference to an entity of the referenced type. A pointer type derived from the referenced type T is sometimes called ‘‘pointer to T’’. [...]
此类型设计用于保存另一种类型(包括指针类型本身)的地址。就像 int
旨在保存整数值,而 double
或 float
则用于保存浮点值。指针类型不需要或强制使用单独的数据结构,它只是定义为能够将地址保存为指针变量的值。
FWIW,头文件 stdint.h
中定义了其他类型,它们能够将地址保存为值:
The following type designates a signed integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer:
intptr_t
The following type designates an unsigned integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer:
uintptr_t
These types are optional.
有关可能的用法,请参阅 What is the use of intptr_t?
关于c - 指针采用什么样的数据结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54140921/