I have this array in main.
int main()
{
int list[] = {1,2,3};
print_list(list);
}
According to the compiler warning the list is NULL
terminated.
When I try to print the list I will print an extra element because sizeof(list) seems to be +1.
void print_list(const int *list)
{
int i;
for(i=0; i < sizeof(list); i++)
printf(list[i]);
}
Should I run the for loop with sizeof(list) - 1
for any case that looks like this?
Or should I check inside the loop for if(list[i] == NULL
?
If my function was not making a pointer out of the list I could use: sizeof(list)/sizeof(list[0])
The function is turning the array into a pointer. So how can I get the accurate size?
It is true my question is very simple and trivial to some. I do believe I have shown enough effort though, please help.
[EDIT] Why isn't the size of an array parameter the same as within main? That is what I know already, it still doesn't answer my question so not a duplicate.