我在YouTube上做了一個練習,結果他得到了同樣的解決方案,但他的代碼能夠獲得指針指向的地址,但是當我編譯並運行它時,出現錯誤分段錯誤(核心轉儲),我以某種方式訪問我不應該訪問的內存?他的代碼是如何運行的,但是我的代碼卻不能運行? 我知道問題出在printf(「num的地址是%d \ n」,* ptr);但是這條線在YouTube上工作,但在我的編譯器中它沒有。爲什麼我要分割(核心轉儲)?
#include <stdio.h>
int print_int_ptr();
int main(){
int num;
num = 10;
print_int_ptr(num);
return(0);
}
/* dont forget that in between the assignment of ptr = &num */
/* Take the address of an integer as a parameter, and print out both the
* address and the integer itself
*/
void print_int_ptr(int *ptr){
printf("The address of num is %d\n", *ptr);
printf("The value of num is %d\n", ptr);
}
編譯程序並打開警告來識別此問題 –