Can the sizeof operator be used to tell the size of an array passed to a function?

No. There’s no way to tell, at runtime, how many elements are in an array parameter just by looking at the array parameter itself. Remember, passing an array to a function is exactly the same as passing a pointer to the first element.

Showing Answers 1 - 6 of 6 Answers

kiran

  • May 8th, 2007
 

Yes, the sizeof operator can tell the size of the array.
It gives the number of bytes reserved for that array.

  Was this answer useful?  Yes

yba

  • Jul 9th, 2007
 

The comment is incorrect, the original answer is correct.

In C, passing an array as a parameter does not pass any information about how many elements are in the array.  sizeof() can tell you the size of the pointer, the size of the type it points to, but it has no way to tell you how many bytes are occupied by the entire array from inside the function.

Try writing code to do it.  If sizeof() is confusing, add code inside the function to do array bounds checking, using only the array parameter, and see how far you get.


Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions