My goal is to print all elements of an array of integers regardless of its length. I would like to print it in Python list format, but then I got this error. Here is my code
int measure(int n[])
{
int num=0;
while (n[num]) { num++; }
return num;
}
void show(int n[])
{
int a = measure(n);
for (int i=0; i<a; i++) {
if (i==0) { printf("[%d,",n[i]); }
else if (i==a-1) { printf(" %d]",n[i]); }
else { printf(" %d,",n[i]); }
}
}
int main(void)
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
show(arr);
}
It is supposed to print this: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
but I got this instead: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1935094528, -1664206169]
then I replace show()
with this:
int i=0;
while (n[i]) {
if (i==0) { printf("[%d,",n[i]); i++; }
else if (n[i+1] == NULL) { printf(" %d]",n[i]); break; }
else { printf(" %d,",n[i]); i++; }
}
and then I got these:
main.cpp:23:28: warning: NULL used in arithmetic [-Wpointer-arith]
23 | else if (n[i+1] == NULL) { printf(" %d]",n[i]); break; }
| ^~~~
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -680101376, -1228044632]
Why does this happen?