Under normal circumstances, define a pointer p pointing to an integer, define an integer variable i, assign it a value of 1, and then assign the variable to *p, that is, *p = i. At this time, the value of p and the value of *p are printed. They are the address of variable i and the value of i respectively. However, if the integer variable i is no longer defined, but the literal 1 is directly assigned to *p, then the return results of p and *p will be empty.
Case 1: Pointer results can be output
#include <iostream>
using namespace std;
int main() {
int* p;
int i = 1;
*p = i;
cout << p << endl;
// 0xef16f0
cout << *p << endl;
// 1
return 0;
}
Case 2: No return value
#include <iostream>
using namespace std;
int main() {
int* p;
*p = 1;
cout << p << endl;
// No return value
cout << *p << endl;
// No return value
return 0;
}
I would like to ask for advice. What is the cause of this situation? What characteristics are based on pointers?
1