我不明白爲什麼我不能打印指針的地址。我知道理解指針是非常重要的,所以任何幫助都是值得讚賞的。傳遞函數指針 - 爲什麼我不能打印地址?
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;
}
當我嘗試使用'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?
'INT&test'是一個參考,不一個指針。 –
只有一個猜測,但我認爲printp不能輸出一個int * ..你可以嘗試先將p轉換爲int,然後再轉換成輸出。 – derHugo
只有一個猜測,但我認爲printp不能輸出一個int * ..你可以嘗試先將p轉換爲int,然後再轉換爲輸出。 – derHugo