Consider the following code:
#include
int main(void)
{
int a[10];
printf(\"%d\",(int)sizeof(a)); //prints 10*sizeof(int) (40 on
sizeof()
returns the size of a type, so the type is what's important.
It also shouldn't be printed with %d
. At the very least, explicitly cast it to unsigned long
or unsigned long long
and use the appropriate format specifier. When teaching C, I had a student get the wrong answer by printing size_t
with %d
as the textbook mistakenly said to do.
Anyway, a
is an array type. In C, array types decay to pointer types if you do almost anything with them or sneeze loudly, so almost anything you do to a
will yield a pointer type. As you've found out, adding or subtracting a number will decay. (After all, an array can't be used in arithmetic, but a pointer can.)