- #1
yungman
- 5,755
- 293
Why this doesn't work?
I know if I do this, it works:
I thought when you declare a pointer int*p; you already allocate a memory for an integer pointed by p already.
Or int*p; only allocate memory for pointer p to store the address, that the address is not valid until using p = &x; to write the address of x into p?
Thanks
C++:
#include<iostream>
using namespace std;
int main()
{
int* p;
*p = 1;//error said uninitialized variable p used.
cout << *p << endl;
return 0;
}
I know if I do this, it works:
C++:
#include <iostream>
using namespace std;
int main ()
{
int* p;
int x = 10;
p = &x;
cout << *p << endl;
return 0;
}
Or int*p; only allocate memory for pointer p to store the address, that the address is not valid until using p = &x; to write the address of x into p?
Thanks
Last edited: