Answer the question
In order to leave comments, you need to log in
How does sizeof calculate the size of an array?
int arr[SIZE] = { 1, 2, 3, 4, 5 };
printf("%zd\n", sizeof(*(arr + 0))); // 4
printf("%zd", sizeof(arr)); // 20
printf("%p\n", &arr[0]);
printf("%p\n", arr);
printf("%zd\n", sizeof(&arr[0])); // Выводится размер указателя
printf("%zd\n", sizeof(arr)); // Выводится размер всего массива, хотя и то и то являются адресами первого элемента
Answer the question
In order to leave comments, you need to log in
sizeof is not a function (even though it looks like it) - it's a size operation.
sizeof is always evaluated at compile time, i.e. in the executable file, instead of calling sizeof, there will already be a calculated constant.
When the compiler knows the size of the array, like in your example, it will return the full size of the array.
But if you pass an arbitrary pointer to sizeof (namely, a pointer, not a static array), it will return the size of the pointer.
A static array is not a pointer. Pointer - at the execution stage, it occupies a place in memory to store the address. A static array takes up space in memory to store the array data.
So a static array is not a pointer. Although often the compiler treats a static array name as a pointer.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question