1
int narr[4] = {0}; int *pnarr = narr; printf("size of pnarr:%d and *pnarr:%d\n", sizeof(pnarr), sizeof(*pnarr)); 

The sizeof(pnarr) return the pointer type size. But the sizeof(*pnarr) return the int type size. Why?

Does the pnarr point at the first element of narr, so I cannot get the array length by pnarr?

How can I get the array length by pointer?

Here is the code of get array length by pointer. Is it right?

int arr_len(int *arr) { int len = 0; while (*arr) { arr++; len++; } return len; } 
1
  • You got it. But I think you cannot simply get the array size that easily... Commented Mar 29, 2020 at 8:08

3 Answers 3

2

What you get is actually what is expected to happen. With

int *pnarr; sizeof (pnarr); 

you get the size of a int* pointer, that is an address (usually 4 or 8 bytes depending on your system architecture). You get it whatever address yiu assign to it.


The arr_len () function you posted in the question will not find the length of the array, because it wrongly assumes that the last element is 0. That is usually not true.

That is done instead with strings (char arrays), which actually are chars terminated by a 0. But in that case it is a standatd, and you are sure that the string won't contain zeros in the middle of it.


In conclusion, if you need the total size in bytes of the array, you can get it from the original array name with

sizeof(narr) 

If you need the number of elements of the array, instead, just use

sizeof(narr)/sizeof(narr [0]) 

So you can't do it with the pointer but you need the original array "reference".

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

2 Comments

narr is a pointer, pnarr is also a pointer. What's the difference of both.
@Jaune Because narr , for the compiler, is also the name of the array. It is some sort of special pointer. It's not an lvalue: you cannot assign to it another address; it will always reference the array, within its scope. pnarr is just a pointer. The compiler knows it points to an integer (in this case) but don't knows how many integers there are.. Furthermore it is a valid lvalue: you can assign a new value to it.
0

That is expected behavior. pnarr is a pointer so sizeof() should return the size of int pointer. Dereferenced pnarr is an int so sizeof() should return the size of int. I suggest you read the documentation first.

Comments

0

The type of the expression *pnarr is int, so sizeof *pnarr == sizeof (int). The type of the expression narr is int [4], so sizeof narr == sizeof (int [4]).

Under most circumstances, the expression narr will be converted (“decay”) from type int [4] to type int * - the exceptions occur when the expression is the operand of the sizeof or unary & operators, or when the array is a string literal that’s used to initialize an array of character type in a declaration.

You cannot get the size of the array from a pointer to the first element - you have to keep track of that manually.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.