-1
int length(int *array[]){
    int len = sizeof(*array) / sizeof(*array[0]);
    printf("The array is of length %d\n",len);
    return len;
}



int main(){
    int array[10] = {1,2,3,4,5,6,7,8,9,10};
    int len = length(&array);
    ...
}

The code above is meant to return the length of an array.However, I get an error when I try to call length from main():

cannot convert int(*)[10] to int** for argument 1 to int length(int**)

How do I pass array to length correctly?

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
Aei
  • 677
  • 6
  • 11
  • 17
  • An array is not a pointer. A pointer is not an array. An array decays into a pointer to its first element when passed to a function. Hence, it is impossible to write a function which returns the length of an array. `int *array[]` is not a pointer-to-array, it would be an array of pointers in other contexts, but inside a function declaration, it's a pointer-to-pointer. – The Paramagnetic Croissant Sep 06 '14 at 19:46
  • When you pass an array to a function, it decays to a pointer to the start of the array. So `sizeof` is not going to tell you the correct length. – Tom Fenech Sep 06 '14 at 19:47
  • 5
    BTW, if you google ["sizeof array incorrect"](https://www.google.hu/search?q=sizeof+array+incorrect), then the **very first four results** are links to identical questions of which this one is a duplicate. Is it really that hard to use Google? – The Paramagnetic Croissant Sep 06 '14 at 19:49
  • 1
    @TheParamagneticCroissant I did not realized the problem lies in using sizeof, otherwise I would have search for that. In any case, I appreciate your original constructive response and I understand my mistake now. – Aei Sep 06 '14 at 19:58
  • In C you cannot pass an array to a function. You can pass a pointer to its first element and the length (this is usual); you can wrap the array inside a structure and pass that structure, ... – pmg Sep 06 '14 at 20:07

2 Answers2

0

Define the function the following way

int length( int ( *array )[10])
{
    int len = sizeof(*array) / sizeof( ( *array )[0]);
    printf("The array is of length %d\n",len);
    return len;
}

In general case if you want to pass an array to a function then you should also pass its size yourself because arrays are converted to pointers to their first elements when they used in expressions including arguments.

So for example these three function declarations are equivalent and declare the same function

void f( int array[10] );
void f( int array[] );
void f( int *array );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0
  • When you do sizeof(array) in main() you will get the size of array which is number of elements * size of int.
  • When you do sizeof(*array) in length() you will get the size of pointer which is size of int.

So when you send the array to length(), you are actually sending the base address of array. And when we pass address of a variable to a function, it will store the address in a pointer. So again if you try to get the size of array you'll get the size of pointer.

How do I pass array to length correctly?

If you want to calculate the length of array you can do that in main().

len = sizeof(array)/sizeof(int);

ani627
  • 5,578
  • 8
  • 39
  • 45
  • This is a good answer. Why was it downvoted? –  Sep 06 '14 at 22:35
  • Even I was surprised to see a down-vote... – ani627 Sep 06 '14 at 22:55
  • The only possibility that makes sense is, "It doesn't answer the OP's question of how to write a `length` function to do this," to which I would reply, "Except in the case of an array terminated with a sentinel value, which isn't necessarily the case and also makes the calculation slower since the entire array must be searched for the sentinel value, you must pass the length to any function since arrays decay to pointers, defeating the function's purpose. A function-like macro could be used, as in `#define length(array) (sizeof array / sizeof *array)`, but not a function." –  Sep 07 '14 at 00:34
  • The size of a pointer may not equal the size of an int. – Matthew Lundberg Sep 21 '14 at 23:12