I don't understand why I can't print the address of the pointer. I know it's pretty fundamental to understand pointers, so any help is appreciated.
void printp(int& test)
{
cout << test << endl;
}
int main()
{
int *p = new int;
*p = 1;
int np = 0;
// printp(p); // why doesn't this print the address of p?
// printp(&p); // why doesn't this print the address of p?
printp(*p); // prints 1
printp(np); // prints 0
return 0;
}
I get the below error when I try using 'printp(p)'.
test.cpp: In function ‘int main()’:
test.cpp:17:10: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]
printp(p); // why doesn't this print the address of p?
^
test.cpp:5:6: note: initializing argument 1 of ‘void printp(int&)’
void printp(int& test)
^~~~~~
test.cpp:17:10: error: cannot bind rvalue ‘(int)p’ to ‘int&’
printp(p); // why doesn't this print the address of p?