0

I am handling a function of type:

 int xyz(int input[])

I do not have access to the main function and therefore have no idea about the size of the array. How can i find the size of the input array? Is there any way to know where the array ends? sizeof(input)/sizeof(int*) is giving 1 as input is basically a pointer.

4
  • sizeof(input) is equivalent to sizeof(int *). So, sizeof(input)/sizeof(int*) will always return 1. Commented Apr 19, 2013 at 9:53
  • You can change the function to take additional parameter storing length. Or, you can store sentinel value at the end of array (like null-terminated strings). Commented Apr 19, 2013 at 9:53
  • possible duplicte stackoverflow.com/questions/5493281/c-sizeof-a-passed-array Commented Apr 19, 2013 at 9:54
  • 1
    This isn't a duplicate - in this question "I do not have access to the main function and therefore have no idea about the size of the array." so a workaround or possible insight to resolve the issue is needed, whereas the linked question assumes you can just start passing the number of parameters from the caller. Commented Apr 19, 2013 at 9:59

3 Answers 3

5

If the caller doesn't provide the size information about the array then there's no way to get the size in function xyz().

You can pass the size information in another variable (or as an elemnt of the array).

int xyx(int abc[], size_t len)
{

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

4 Comments

Does int[] reduce to an integer pointer is statements like "int xyz(int input[])"?Are such statements legal in C?I mean,can we use input[] as a parameter?If yes, does it reduce to a pointer?
Unless I am wrong we can't pass arrays in C functions eh?
@SheerFish yes and yes and yes.
@SheerFish Yes. int xyz(int input[]){} is a legal C. int xyz(int input[]){} and int xyz(int *input){} are absolutely equivalent. When an array is passed to a function it decays as a pointer.
3

No... there's no (portable) way from within the called function, so it's normal in this situation (i.e. when no number-of-elements parameter is passed) for callers to adopt some convention such as providing a sentinel value in the last used element or guarantee a set number of elements. If a sentinel is used, you might be able to prove to yourself that this was being done by checking the calling code in a debugger.

2 Comments

does sizeof(input)/sizeof(int*) reduce to one because "input" is nothing but an integer pointer as well?
Exactly - for function parameters the array is accepted as a pointer. sizeof(array) only works when the array dimension is known in that part of the program. Consider that sizeof is always evaluated at compile time, and it's entirely possible for a function f(int a[]) to be called twice with different sized arrays, so it's an impossible situation. (In C++ you can use templates: template <size_t N> void f(int (*)[N]) { /* now N holds the array size */ }.)
0

If you pass an array to a function and don't provide the length, you can't find it, because the array decays to as pointer whose size is always 32 (64) bit.

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.