This is my c code:
#include <stdio.h>
int main(){
int x[] = {6,1,2,3,4,5};
int *p=0;
p =&x[0];
while(*p!='\0'){
printf("%d",*p);
p++;
}
return 0;
}
When run the output is 612345-448304448336
What are the digits after the minus sign and why is my code giving this?
The condition
*p != '\0', which is the same as*p != 0, is never met because your array doesn’t contain an element of value0, and thus you overrun the array bounds and step into undefined behaviour.Instead, you should control the array range directly: