For the program :
#include<stdio.h>
int main(void)
{
int (*a)[2];
int b[5];
printf("sizeof(int) : %zu\n", sizeof(int));
printf("sizeof(int*) : %zu\n", sizeof(int*));
printf("sizeof(b) : %zu\n",sizeof(b));
printf("sizeof((int*)b) : %zu\n",sizeof((int*)b));
printf("sizeof(&b[0]) : %zu\n",sizeof(&b[0]));
printf("sizeof(a) : %zu\n",sizeof(a));
printf("sizeof(a[0]) : %zu\n",sizeof(a[0]));
printf("sizeof(a[1]) : %zu\n",sizeof(a[1]));
return 0;
}
Output is :
sizeof(int) : 4 -> Fact 1
sizeof(int*) : 8 -> Fact 2
sizeof(b) : 20 -> Case 1
sizeof((int*)b) : 8 -> Case 2
sizeof(&b[0]) : 8 -> Case 3
sizeof(a) : 8 -> Case 4
sizeof(a[0]) : 8 -> Case 5
sizeof(a[1]) : 8 -> Case 6
Questions/Observations (in Case Order ) :
Is Case 1 output 20 because
b
was declared as an array of integers ieint[]
? The total block in bytes is returned as confirmed by Fact1. Isn't it?I guess casting
b
toint*
made the difference here. Hereb
is considered a pointer. I confirmed this using Fact2. Right or wrong?&b[0]
decays to a pointerb
. The output coincides with Fact2 .I expected 16 here but I got 8 as the output. I concluded that this is because
a
is afterall a pointer and the output coincides with Fact2 . I got the output similar to Question 2.a[0]
is pointer. The output coincides with Fact2a[1]
is pointer. The output coincides with Fact2
Please answer the questions and correct me if any of the observations are wrong.