C语言、指针算术

标签 c pointers

我有以下代码

void main(void) {
    int data = 0x5555;
    int* dataptr = &data;
    int** dataptrptr = &dataptr;
    int*** dataptrptrptr = &dataptrptr;
    printf("%d\n", ***dataptrptrptr);
}

data位于地址0xabcd

dataptr 位于地址 0x1234

dataptrptr 位于地址 0x8888

dataptrptrptr 位于地址 0xffff

*((*dataptrptrptr) + 4) 0x8888 + 4 = 0x888c 正在读取有效地址吗?

另外,表达式**dataptrptrptr的值是0x1234吗?

最佳答案

Is the effective address being read by *((*dataptrptrptr) + 4) 0x8888 + 4 = 0x888c?

也许 - 这取决于

*dataptrptrptr0x8888,但地址并不像整数那么简单。

将 4 作为整数加到 0x8888 上是 0x888C。

作为指针,将 4 添加到 int** 0x8888 将在内存中进一步形成地址 4 int** 指针。

如果 int ** 指针为 8 个字节,并且如果地址在字节地址上递增,则预期总和为 int **0x88A8。

如果 int ** 指针为 4 个字节,并且如果地址在字节地址上递增,则预期总和为 int **0x8898。

如果一个int **指针是N个字节并且if int **存在于一个特殊的地方在内存中,地址以 N 字节 block 为单位,预期总和为 int** 0x888C。

但还存在其他可能性:0x8890、pointer_street:0x0004、未定义行为

指针数学不是整数数学。


表达式**dataptrptrptr的值是int *指针0x1234。

关于C语言、指针算术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61372297/

相关文章:

c - 在 C 中为宏使用预处理器语句

c - 控件的两个资源 ID?

C 将两个小字符串连接成一个大字符串

go - 如何在 Go 中复制接口(interface)值?

c++ - C++ 中重载的指针问题?

c - 是否可以在接受事件时执行 epoll?

c++ - 如何在三星 galaxy tab 等 Android 设备上运行 c 或 c++ 程序?

c - 为什么 feof() 在任何操作之前返回 0?

c++ - Swig:如何将指针列表从 C++ 返回到 Tcl

C:用指针返回数组程序中的偶数,为什么我的程序打印结果后崩溃