c - How is pointer to array different from array names? -
i reading more arrays vs pointers in c , wrote following program.
#include <stdio.h> int arr[10] = { } ; typedef int (*type)[10] ; int main() { type val = &arr ; printf("size %lu\n", sizeof(val)) ; printf("size of int %lu\n", sizeof(int)) ; }
if, execute program, sizeof(val)
given 8 , sizeof(int)
given 4.
if val
pointer array 10 elements, shouldn't it's size 40. why sizeof(val)
8 ?
if val pointer array...
yes, is, , sizeof(val)
produces size "pointer array", not array itself.
...shouldn't it's size 40.?
no, sizeof(val)
calculates size of operand, "pointer" here. in platform, size of pointer seems 64 bits, i.e., 8 bytes. so, gives 8
.
also, mentioned, use %zu
print size_t
, type produced sizeof
operator.
Comments
Post a Comment