mzoz
mzoz

Reputation: 1353

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.

Does it indicate that the array pointed by array_ptr is stored in heap? But the dynamically allocated memory chunk pointed by buff is also supposed to be in heap and its value changes, why is that? Thanks!

Upvotes: 0

Views: 164

Answers (2)

Shlomi Agiv
Shlomi Agiv

Reputation: 1198

array_ptr is uninitialized pointer, and gets an undefined value(in your case 0).

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

ptr is also an uninit pointer, which in your case points to garbage.

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

Upvotes: 1

Does it indicate that the array pointed by array_ptr is stored in heap?

No. array_ptr points to nothing, neither on the stack nor on the heap.


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. 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. Thus, You´ll get any kind of Undefined Behavior/ unpredictable results.

You need to initialize array_ptr with, f.e. the address of the first int object of array:

int (*array_ptr)[100] = array;

The same goes for the pointer ptr with:

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.

But the dynamically allocated memory chunk pointed by buff is also supposed to be in heap and its value changes, why is that?

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];.

Upvotes: 0

Related Questions