0

In C I can write a function declaration in 2 different ways (Which I was told are totally equal)

void test (int *arr);

void test (int arr[]);

But why we every need to use the first one? I mean the latter allows us to get the number of elements in the array using sizeof() something which isn't possible in the first one.

  • *I mean the latter allows us to get the number of elements in the array using `sizeof()`* **No**, it does not. – Andrew Henle Jul 15 '20 at 09:52
  • Many have already told you that your assumptions are wrong. Here another thing you can consider: You write: "But why we every need to use the first one?" For passing a single pointer to a function, perhaps. Would you really want to write `void test (int arr[]);` for a function taking a single pointer? – Support Ukraine Jul 15 '20 at 09:53
  • 1
    Linked duplicate does not actually answer the question, which is *not* how to get size of an array parameter. – hyde Jul 15 '20 at 10:51

3 Answers3

3

I mean the latter allows us to get the number of elements in the array using sizeof() something which isn't possible in the first one.

No, that's not possible for the latter, too. Here is a misunderstanding of yours. Both notations are absolutely equivalent as function parameter and denote a pointer. arr[] does not denote an array nor a pointer to an array explicitly.

Take a look at here:

C pointer notation compared to array notation: When passing to function

You can only get the size of the array if you use sizeof() at the array name itself inside of the scope the array is visible or pass another argument/parameter to the function which contains the information about the size of an array from the previous sizeof() use.

You cannot use sizeof() at a pointer to an array to get the array size, neither in the function where the array is declared nor in another.

But why we every need to use the first one?

To simple symbolize that it is a single pointer and not an array as this confusion in fact already brought you to here. :-)

1

I mean the latter allows us to get the number of elements in the array using sizeof() something which isn't possible in the first one.

This statement is wrong. Both versions are in fact identical. The function does not know the size of the array and you cannot use sizeof to get the size of the array. Instead you will only get the size of the pointer which the passed array decays to.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • But why, in main if I declare an array I can get its size using sizeof() –  Jul 15 '20 at 10:07
  • 1
    @BigSur Because the declaration of the array is in the same function/scope you use `sizeof()` and don't use a pointer to address the array. That's a completely different scenario. You can't use `sizeof()` at a pointer to an array to get the array size. – RobertS supports Monica Cellio Jul 15 '20 at 10:22
0

I mean the latter allows us to get the number of elements in the array using sizeof()

No, it is not right information at all as to functions. After passing an array to a function taking the array as a parameter, the array decays into pointer thereby yielding size of the type's pointer type rather than that of the type per se.

For example for int type,

sizeof int
sizeof *int