简体   繁体   English

C打印数组指针的地址问题

[英]C print address of pointer to array problem

When I run the following code:当我运行以下代码时:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int array[100];
    int (*array_ptr)[100];
    void *buff = malloc(500);
    int *ptr;
    printf("array: %p \narray+1: %p\n", array, array+1);
    printf("array_ptr: %p \narray_ptr+1: %p\n", array_ptr, array_ptr+1);
    printf("buff: %p\n", buff);
    printf("ptr: %p\n", ptr);
}

the result is like this:结果是这样的:

array: 0x7fffe6dc6bd0
array+1: 0x7fffe6dc6bd4
array_ptr: (nil)
array_ptr+1: 0x190
buff: 0x1f80260
ptr: 0x7fffe6dd417c

I run it multiple times, array , array+1 , buff and ptr all change values randomly, but array_ptr and array_prt+1 never change, although the pointer arithmetic result 0x190 is as expected.我多次运行它, arrayarray+1buffptr都会随机更改值,但array_ptrarray_prt+1永远不会改变,尽管指针运算结果0x190符合预期。

Does it indicate that the array pointed by array_ptr is stored in heap?它是否表示array_ptr指向的数组存储在堆中? But the dynamically allocated memory chunk pointed by buff is also supposed to be in heap and its value changes, why is that?但是buff指向的动态分配的内存块也应该在堆中并且它的值发生了变化,这是为什么呢? Thanks!谢谢!

array_ptr is uninitialized pointer, and gets an undefined value(in your case 0). array_ptr是未初始化的指针,并获取未定义的值(在您的情况下为 0)。

array_ptr+1 is sizeof(int)*100 = 400 = 0x190 more than array_ptr array_ptr+1是 sizeof(int)*100 = 400 = 0x190 比 array_ptr

ptr is also an uninit pointer, which in your case points to garbage. ptr也是一个 uninit 指针,在你的情况下它指向垃圾。

You need to initialize pointers after they're defined to get any valid results您需要在定义指针后对其进行初始化以获得任何有效结果

To your question, array is on the stack, buff is on the heap, and ptr / array_ptr are uninitialized will give you garbage or segmentation fault if you try to access their data对于您的问题, array在堆栈上, buff在堆上,并且ptr / array_ptr未初始化会在您尝试访问它们的数据时给您垃圾或分段错误

Does it indicate that the array pointed by array_ptr is stored in heap?它是否表示array_ptr指向的数组存储在堆中?

No. array_ptr points to nothing, neither on the stack nor on the heap.不。 array_ptr指向任何东西,既不在堆栈上也不在堆上。


int (*array_ptr)[100];

array_ptr is one pointer to an array of 100 int objects, but with this statement you do not create an array with 100 int objects by which array_ptr is pointing to the first element of this array. array_ptr一个指向 100 个int对象数组的指针,但使用此语句,您不会创建一个包含 100 个int对象的数组,其中array_ptr指向该数组的第一个元素。 It only creates the pointer itself.它只创建指针本身。

With:和:

printf("array_ptr: %p \narray_ptr+1: %p\n", array_ptr, array_ptr+1);

You are trying to print the addresses of objects the pointer array_ptr and its offset by 1 point to, but this isn´t possible since array_ptr is not initialized to point to such objects.您正在尝试打印指针array_ptr及其偏移 1 指向的对象的地址,但这是不可能的,因为array_ptr未初始化为指向此类对象。 Thus, You´ll get any kind of Undefined Behavior / unpredictable results.因此,您会得到任何类型的未定义行为/不可预测的结果。

You need to initialize array_ptr with, fe the address of the first int object of array :您需要使用array的第一个int对象的地址来初始化array_ptr

int (*array_ptr)[100] = array;

The same goes for the pointer ptr with:指针ptr

printf("ptr: %p\n", ptr);

ptr needs to have an address of an object it points to in order to show the address of that object. ptr需要有它指向的对象的地址才能显示该对象的地址。

But the dynamically allocated memory chunk pointed by buff is also supposed to be in heap and its value changes, why is that?但是buff指向的动态分配的内存块也应该在堆中并且它的值发生了变化,这是为什么呢?

This is a completely different thing.这是完全不同的事情。 With malloc you do allocate memory on the heap (if it was successful of course) and you do get a pointer to that memory back, which isn´t the case with int (*array_ptr)[100];使用malloc确实在堆上分配内存(当然,如果它成功的话)并且您确实获得了指向该内存的指针,而int (*array_ptr)[100];不是这种情况int (*array_ptr)[100]; . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM