Why is the output the same for both &arr and arr in these c code snippets?
Why do Program 1 and Program 2 both output 40? The reason for Program 2 to output 40 is very clear to me but why doesn't the & in Program 1 make a difference? My initial expectation was that Program 1 and Program 3 would behave similarly since both involve calculating the address of an int** instead of an int*. Is there no underlying pointer to the array when malloc or calloc is not used? Program 1: #include <stdio.h> int main() { int arr[3] = {40, 41, 42}; int *ptr = (int*)(&arr); // Notice the &. printf("%d", *(ptr)); return 0; } Program 2: #include <stdio.h> int main() { int arr[3] = {40, 41, 42}; int *ptr = (int*)(arr); // notice the lack of &. printf("%d", *(ptr)); return 0; } // Outputs unpredictable values like 25322080(address of pointer instead of 40) Program 3: #include <stdio.h> #include <stdlib.h> int main() { int *arr = (int*)malloc(3 * sizeof(int)); arr[0] = 40; arr[1] = 41; arr[2] = 42; int *ptr = (int*)(&arr); printf("%d", *(ptr)); free(arr); return 0; }