When I do this, it prints out "2" perfectly.
int main()
{
int *p;
int x = 2;
*p = x;
cout << *p;
}
But when I first initialized *p to be null, the program crashes.
int main()
{
int *p=0;
int x = 2;
*p = x;
cout << *p;
}
I want to ask what does the first program even successfully run in the first place, why can a value be assigned to an uninitialized pointer?
[EDIT] My question is actually related to this past exam question that I got. You can tick more than one answer and it seems (b) & (c) both are correct. But now I know whether (c) works is purely due to luck.
See Question&Answers more detail:os