Page 46 - C-Language
P. 46
An common short cut to the above loop is to use memset() from <string.h>. Passing array as shown
below makes it decay to a pointer to its 1st element.
memset(array, 0, ARRLEN * sizeof (int)); /* Use size explicitly provided type (int here). */
or
memset(array, 0, ARRLEN * sizeof *array); /* Use size of type the pointer is pointing to. */
As in this example array is an array and not just a pointer to an array's 1st element (see Array
length on why this is important) a third option to 0-out the array is possible:
memset(array, 0, sizeof array); /* Use size of the array itself. */
Array length
Arrays have fixed lengths that are known within the scope of their declarations. Nevertheless, it is
possible and sometimes convenient to calculate array lengths. In particular, this can make code
more flexible when the array length is determined automatically from an initializer:
int array[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
/* size of `array` in bytes */
size_t size = sizeof(array);
/* number of elements in `array` */
size_t length = sizeof(array) / sizeof(array[0]);
However, in most contexts where an array appears in an expression, it is automatically converted
to ("decays to") a pointer to its first element. The case where an array is the operand of the sizeof
operator is one of a small number of exceptions. The resulting pointer is not itself an array, and it
does not carry any information about the length of the array from which it was derived. Therefore,
if that length is needed in conjunction with the pointer, such as when the pointer is passed to a
function, then it must be conveyed separately.
For example, suppose we want to write a function to return the last element of an array of int.
Continuing from the above, we might call it like so:
/* array will decay to a pointer, so the length must be passed separately */
int last = get_last(array, length);
The function could be implemented like this:
int get_last(int input[], size_t length) {
return input[length - 1];
}
Note in particular that although the declaration of parameter input resembles that of an array, it in
fact declares input as a pointer (to int). It is exactly equivalent to declaring input as int *input.
https://riptutorial.com/ 22

