Take this piece of code as an example:
#include <stdio.h>
void print_array(size_t size, unsigned (*myarray)[])
{
printf("Array size: %lu\n", size);
for (size_t i = 0; i < size; ++i)
printf("%u ", (*myarray)[i]);
printf("\n");
}
int main() {
unsigned myarray[] = { 9, 8, 7, 6, 5 };
print_array(sizeof(myarray) / sizeof(unsigned), &myarray);
return 0;
}
When compiled with clang analyzer (via gcc 10.1.0) the warning is:
src/main.c:7:3: warning: 2nd function call argument is an uninitialized value
printf("%u ", (*myarray)[i]);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
The output is:
Array size: 5
9 8 7 6 5
What is the mistake in this piece of code and what is the proper way to pass pointers to arrays in C?