Suppose if int A[5]
is declared then variable A
will be pointer to the A[0]
. Which means A
is just a pointer and A
stores the base address of array A[5]
. Then how come sizeof(A)
gives answer as 20

- 1
-
1Which language is this about? I presume C? – Martin Tournoij Jul 09 '18 at 23:49
-
You should add the C or C++ tag so people can more easily find the question. Also, you can format small bits of code with backticks like `\`this\``. – eesiraed Jul 10 '18 at 00:02
-
Possible duplicate of [Is an array name a pointer?](https://stackoverflow.com/questions/1641957/is-an-array-name-a-pointer) – davmac Jul 10 '18 at 13:10
2 Answers
A
isn't a pointer to the first element. It is an int[5]
, or a five element array of ints (the size is part of the type). It can decay into a pointer to the first element when you do stuff like pass it to a function taking a pointer.

- 4,626
- 4
- 16
- 34
-
if A[10] is declared then why do this statements give same answer =>printf("%d", A[i]) and printf("%d", *(A+i)); – satya Jul 11 '18 at 16:57
-
@satya Using the subscript operator is one of the situations where the array decays into a pointer. `A[i]` is exactly the same as `*(A + i)` here. – eesiraed Jul 11 '18 at 22:23
Contrary to what you might have heard, arrays are not the same as pointers.
In most contexts, the name of an array will decay into a pointer to the first element, such as being passed to a function or as the subject of pointer arithmetic.
One of the cases where this decay does not happen is when the array is the subject of the sizeof
operator. In that case the operator returns the full size of the array in bytes.
This is detailed in section 6.3.2.1p3 of the C standard:
Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type "array of type" is converted to an expression with type "pointer to type" that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.
Section 6.5.3.4p4, which details the sizeof
operator, additionally states:
When
sizeof
is applied to an operand that has typechar
,unsigned char
, orsigned char
, (or a qualified version thereof) the result is 1. When applied to an operand that has array type, the result is the total number of bytes in the array. When applied to an operand that has structure or union type, the result is the total number of bytes in such an object, including internal and trailing padding.
If you had some something like this:
int A[5];
int *B;
B = A;
printf("sizeof(B)=%zu\n", sizeof(B));
You would get the size of an int *
on your system, most likely either 4 or 8.

- 205,898
- 23
- 218
- 273
-
int A[5] ...then A[i] is equal to *(A+i).....because A which is array name points to first element i.e. A[0]....then why does "sizeof(A) " returns 20 which is size of whole array?? – satya Jul 11 '18 at 16:53
-
@SatyaprakashYelgatte `A` doesn't point to the array because it is **not** a pointer. `A` **is** the array, so `sizeof(A)` gives the size in bytes of the whole array. – dbush Jul 11 '18 at 16:55