3
int doSomething(int n, int arr[n])

What confuses me here is the reusing of the n variable inside the parameter list. Can anyone explain why this is valid and what are the use cases?

Michael Munta
  • 207
  • 2
  • 16
  • In given case `n` is ignored, see given answer – one usually still adds it for writing self-documenting code to let everybody know the pointer being expcted to point to an array of length `n` – or better: *at least* `n`. – Aconcagua Nov 25 '21 at 08:05

2 Answers2

1

C allows several syntaxs for passing an array pointer including:

 int* arr, int arr[] or int arr[10]  

However ALL of these are equivalent to the first - that is the compiler sees only an int pointer and does not use any size that may have been specified in the array square brackets to enforce integrity on the size of the array passed. In all the above cases the compiler will generate:

void __cdecl foo(int * const)

The only way to specify the size of an array is, as you have done, pass a separate parameter indicating the size/length of the array.
In the example you cite the n in arr[n] is not used by the compiler to maintain any integrity on the array bounds - in the example it is only included as an indication that the first parameter is the size. A less controversial form of the same code might be just to state that more explicitly:

int doSomething(int arr_len, int arr[])

In terms of what is the best convention or preference for passing an pointer and its array size see this question.

Ricibob
  • 7,505
  • 5
  • 46
  • 65
  • 1
    @MichaelMunta Note, though, that this only applies for the *outermost* index; in `size_t n, int arr2D[][n]` the parameter `arr2D` is a pointer to an array of (dynamic) size `n` (variable length array – VLA), the pointer's actual type is `int (*)[n]`. – Aconcagua Nov 25 '21 at 08:03
  • So, it is just a convention for a human to know? It doesn't actually *do* anything? – Michael Munta Nov 25 '21 at 10:54
  • 2
    If you think the compiler ignores any size in the array brackets of a parameter, take a look at [what `#include ` / `int main(int argc, char *argv[printf("Hello, world.\n")]) { }` prints](https://godbolt.org/z/avdfqGzr1). While the compiler adjusts the array declaration to be a pointer declaration, that does not mean it ignores the size. – Eric Postpischil Nov 25 '21 at 11:22
  • @EricPostpischil Nice example! Yes "ignor" clearly an over simplification... – Ricibob Nov 25 '21 at 11:43
  • @MichaelMunta The example from EricPostpischil suggests that it is not ignored however in the context of and for the purpose of your question it is an indicator only - many would pref the pointer int* or [] notation - but that's a subjective question of taste - see link in answer. – Ricibob Nov 25 '21 at 11:47
  • Not sure I understand your first comment and Eric's example. Can you elaborate? – Michael Munta Nov 25 '21 at 12:39
  • @MichaelMunta For the purpose of your question with regard to array bounds it is essentially ignored. EricPostpischil was just pointing out that technically it is not ignored as such. The key thing is that the function signature generated will always be just a int* int const. – Ricibob Nov 25 '21 at 15:32
1

It is valid.

For this specific case it is equivalent to:

int doSomething(int n, int arr[]);
int doSomething(int n, int *arr);

because the array parameters are automatically transformed into pointers to arrays' elements. In general there are a few differences between array-like and pointer declarations of parameters. See link for more details.

IMO, the better usage should be:

int doSomething(int n, int arr[static n]);

This static n tell the compilers that at least n elements pointer by arr are valid. Moreover it makes the declaration visually different from a declaration of array what helps to avoid surprising issues when sizeof arr is used.

tstanisl
  • 13,520
  • 2
  • 25
  • 40