I am a newbie in C, but I have already watched dozens of videos on YT about how to understand pointers... Unfortunately, it seems my stupid brain is not getting the point about pointers. Could anyone help me to understand why I am getting such values in the output?
The output is: 1-40 256-296 64-104
#include <stdio.h>
int main() {
int arr1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int arr_size = sizeof(arr1) / sizeof(arr1[0]);
int arr2[arr_size];
int arr3[arr_size];
int *ptr1 = arr1;
int *ptr2 = arr2;
int *ptr3 = arr3;
printf("%u-%u %u-%u %u-%u\n", *ptr1, sizeof(arr1), *ptr2, *ptr2+sizeof(arr2), *ptr3, *ptr3+sizeof(arr3));
return 0;
}
Thanks for help!
The arrays
arr2
andarr3
are not initialized. So their elements have indeterminate values. Outputting uninitialized elements invokes undefined behavior.Also to output a value of the type
size_t
you need to use the conversion specifierzu
instead ofu
in a call ofprintf
.So the only valid call of
printf
will look the following wayThis call outputs the value of the first element of the array
arr1
by means of the pointerptr1
that (value) is equal to1
and the size of the array itself that can be equal to40
provided thatsizeof( int )
is equal to4
.