c - 指针采用什么样的数据结构?

标签 c pointers data-structures

为什么只有指针变量才能保存任何其他变量的地址?如果我们将任何变量的地址存储在简单变量中,那么它将将该地址存储为值。指针变量设计成什么样的数据结构?

最佳答案

我相信您忽略了一点:指针也是一种类型,一种单独的类型,就像标准整数类型、浮点类型等一样。

引用 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 旨在保存整数值,而 doublefloat 则用于保存浮点值。指针类型不需要或强制使用单独的数据结构,它只是定义为能够将地址保存为指针变量的

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/

相关文章:

c++ 无法将指针数组初始化为 null c++

algorithm - 存储树和图的数据?

c++ - 使 GUI 从 Windows 命令行运行

c - 实现 NTP 服务器端

python - 为 Python 3.x 编译 Python C 模块时未定义 PY_MAJOR_VERSION

c++ - 为什么在创建入栈函数时使用指向栈的指针?

c - fgets 无法在 1 行上显示多个 "%"

c - 递归链表反转

python - 在将大型数据集存储到数据库之前在 Python 中定义它们

haskell - Haskell 有没有一种简单的方法来实现快速优先级队列?