-5

Given pointer to an array as parameter to a function in C language, how to find its length?

int* mymap(int (*func)(int), int* Array) {
    int size = sizeof(Array); 
    // giving 8 (which is size of int*),but I want length of the array which I gave is 5.
    int* B = (int*) malloc(sizeof(int)*size);
    printf("size %d\n", size);
    for (int i = 0; i < size; ++i)
    {
        B[i] = func(Array[i]);
    }
    return B;
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • 5
    You can't. Think about the length of the array as not being one of it's properties and store it somewhere while you need to use it. If you really need to pass just one parameter to any function handling this array, simply create a struct where you store the data with a pointer and the array size in another member. – Iharob Al Asimi Feb 17 '18 at 14:08
  • 2
    Duplicate alarm! – alk Feb 17 '18 at 14:11
  • This is finding length using pointer to an array, which is not possible as given in the answer below – Mahith Bhima Feb 17 '18 at 14:15

1 Answers1

3

You can't do that. sizeof works for array the way you expect it to be - returning the size of the array. Even if you passed array which will be converted to pointer to first element to it (Known as array decay) so basically you are not using sizeof over an array - rather over an decayed pointer to it. As a result now when you use it with sizeof operator it gives you sizeof pointer not size of array. Solution is to pass the length as argument to the function.

int array[] = {1, 2, 3};
size_t size = sizeof array / sizeof array[0];

..
f(array, size);
user2736738
  • 30,591
  • 5
  • 42
  • 56
  • ok,I am going for passing length as parameter. – Mahith Bhima Feb 17 '18 at 14:11
  • @IharobAlAsimi.: Yes now check. – user2736738 Feb 17 '18 at 14:11
  • @IharobAlAsimi.: Like before I still mess up the spacing - is there any place where it is documented where I should use spaces...Thanks for the edit. – user2736738 Feb 17 '18 at 14:14
  • Nowhere, it's a matter of taste. You just need to make sense of it to yourself. For instance, a space after a coma makes a lot of sense and separates the *items* naturally, spaces around operators are very important because they let you see the operator immediately. Also, don't use too many, use just those that make the code as readable as possible. – Iharob Al Asimi Feb 17 '18 at 14:17
  • @IharobAlAsimi.: Well I see..it's at times confusing to guess about while answering. Get your point. – user2736738 Feb 17 '18 at 14:21
  • And apparently `sizeof` is an **operator** and *not* a function, so it does ***not require** parenthesis around it's argument*. Still if one needs the size of a type (not of a variable), the C language requires the type to be enclosed by parenthesis. So I'd prefer to write: `size_t s = sizeof s;` and `s = sizeof (size_t);` Note the spaces. – alk Feb 17 '18 at 14:44
  • @alk.: Yes that I know but still gave...contradicted myself. Ah I see. You are mentioning in context of formatting..yes will try that. (You meant `sizeof (size_t)` ) – user2736738 Feb 17 '18 at 14:46