我試圖將地址作爲參數傳遞給練習的過程。它不工作,所以我做了這個簡單的例子來發現什麼是錯的:使用scanf()和指針
int main(int argc, const char * argv[]) {
int newCounter = 0; // I want to get the value of this using its address
int *address; // The pointer
printf("%p\n", &newCounter); // Printing the address
scanf("%p", address); // We insert the address manually
printf("%d\n", *address);
}
當我運行這個程序,我得到一個消息Segmentation fault: 11
手動並按Enter鍵插入的newCounter
地址後,沒有人知道這段代碼有什麼問題?
一個簡單而重要的問題:**你有沒有試過編寫完整的警告?**因爲我期望即使MSVC++真的大聲抱怨。 – Deduplicator 2014-11-05 17:36:46
其實這一行:scanf(「%p」,address);試圖在變量地址指向的地方放置一個地址值,目前並不指向任何特定的事物。但是,如果要更改地址變量中的值,請使用:scanf(「%p」,&address); – user3629249 2014-11-06 07:58:08