This question was inspired by this snipped from a C book I've been working through
Where is says:
int num;
int *pi=0; // Zero referes to the null pointer, NULL
pi = #
*pi = 0; // Zero refers to the integer zero
We are accustomed to overloaded operators, such as the asterisk used to declare a pointer, to dereference a pointer, or to multiply. The zero is also overloaded. We may find this discomforting because we are not used to overloading operands.
So I did a bunch of investigating with the debugger and my answer is what the debugger showed me. It's undefined behavior, I get that, but I'm not writing a program I'm trying to figure something out. Whatever the case, the pointer is initialized to 1 every time I run it, I'm not depending on that in some calculation, it's what the debugger repeatedly shows.
This is all just one question really, but I had to write more or it wouldn't accept it. Why does *p=0 have different effects at different times? How is 0 overloaded? How can I tell if *p=0 is making p a null pointer or if it is assigning 0 to a variable that p is pointing to? And how does the compiler know to assign a null at the address of p rather than the address that p points too?
$ cat prac.c
#include <stdio.h>
int main()
{
int num;
int *p;
printf("0) %p is %d\n",p,*p);
*p=0; //Why is this......
printf("1) %p is %d\n",p,*p);
num=5;
p=#
printf("2) Now %p is %d\n",p,*p);
*p=0; //....different than this
printf("3) p is not null, %p : %d\n",p,*p);
return 0;
}
$ ./prac
0) 0x7ffeb36b5c30 is 1 //pointer is initialized to 1, not-null, definitely out-of-bounds though
1) 0x7ffeb36b5c30 is 0
2) Now 0x7ffeb36b5b3c is 5
3) p is not null, 0x7ffeb36b5b3c : 0