2
int a[17];
size_t n = sizeof(a) / sizeof(int);

In C it is most common to find a length of an array like this and it is pretty clear. My question though is that the following wouldn't work:

int days[] = {1,2,3,4,5};
int *ptr = days;
size_t n = sizeof(ptr) / sizeof(int);

What I suppose is happening here is that ptr is just a normal 8-byte address pointing to the first element of the array, so it won't give us the real length of the array. But in the first example, sizeof(a), a is also just a pointer pointing to the first element of the array. So what is the difference here and why does one work but the other not?

3
  • 2
    Contrary to what some people will tell you, arrays are not pointers. Commented May 24, 2021 at 8:12
  • 2
    "...a is also just a pointer pointing to the first element of the array" no, no, no... a is not a pointer- it's an array. ptr is a pointer but days is an array Commented May 24, 2021 at 8:14
  • 1
    a is of type int[17], which is statically known to the compiler; this type decays to int* in many contexts, but not all, and sizeof is one of the non-decaying contexts. The compiler in general can't know what ptr is pointing to. (In this example, an optimising compiler could probably infer that it points to length-5 array days, but imagine if ptr was changed around in various complicated ways between this initial assignment and the sizeof ptr.) Commented May 24, 2021 at 8:15

2 Answers 2

2

What I suppose is happening here is that ptr is just a normal 8-byte address pointing to the first element of the array, so it won't give us the real length of the array

You are right.:)

But in the first example, sizeof(a), a is also just a pointer pointing to the first element of the array. So what is the difference here and why does one work but the other not?

Array designators are converted to pointers to their first elements in expressions except using them as operands of the operator sizeof or the operator &.

From the C Standard (6.3.2.1 Lvalues, arrays, and function designators)

3 Except when it is the operand of the sizeof 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.

So in this expression

sizeof(a)

the array a is not converted to a pointer.

If you will write for example

sizeof( a + 0 )

then you will get the size of a pointer.

Sign up to request clarification or add additional context in comments.

Comments

2

In the first case, you have an array. sizeof(a) give the number of bytes required to store it.

In the second case, you have a pointer. sizeof(ptr) gives the number of bytes required to store the pointer (Depends on architecture, usually 4 or 8).

The compiler knows the array size but doesn't know how many elements are pointer by a pointer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.