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!
array_ptris uninitialized, it doesn't point anywhere, its value will be indeterminate. Same withptr.malloc.array_ptr+1's value still available?malloc. But note that the storage for the variablebuffitself isn't on the heap, it only points to the heap.#include <stdlib.h>in order to usemalloc.