when do we need to pass the size of array as a parameter when do we need to pass the size of array as a parameter arrays arrays

when do we need to pass the size of array as a parameter


First, an array passed to a function actually passes a pointer to the first element of the array, e.g., if you have

int a[] = { 1, 2, 3 };f(a);

Then, f() gets &a[0] passed to it. So, when writing your function prototypes, the following are equivalent:

void f(int arr[]);void f(int *arr);

This means that the size of the array is lost, and f(), in general, can't determine the size. (This is the reason I prefer void f(int *arr) form over void f(int arr[]).)

There are two cases where f() doesn't need the information, and in those two cases, it is OK to not have an extra parameter to it.

First, there is some special, agreed value in arr that both the caller and f() take to mean "the end". For example, one can agree that a value 0 means "Done".

Then one could write:

int a[] = { 1, 2, 3, 0 }; /* make sure there is a 0 at the end */int result = f(a);

and define f() something like:

int f(int *a){    size_t i;    int result = 0;    for (i=0; a[i]; ++i)  /* loop until we see a 0 */        result += a[i];    return result;}

Obviously, the above scheme works only if both the caller and the callee agree to a convention, and follow it. An example is strlen() function in the C library. It calculates the length of a string by finding a 0. If you pass it something that doesn't have a 0 at the end, all bets are off, and you are in the undefined behavior territory.

The second case is when you don't really have an array. In this case, f() takes a pointer to an object (int in your example). So:

int change_me = 10;f(&change_me);printf("%d\n", change_me);

with

void f(int *a){    *a = 42;}

is fine: f() is not operating on an array anyway.


WHen an array is passed in C or C++ only its address is passed. That is why the second case is quite common, where the second parameter is the number of elements in the array. The function has no idea, only by looking at the address of the array, how many elements it is supposed to contain.


you can write

void f( int *arr, int size )

as well, having latter (size) allows to not step outside the array boundaries while reading/writing to it