你崩潰無關與strtol
。問題是您取消引用具有值NULL
的指針。這是非法的,並導致崩潰(seg故障)。
你的問題是在這裏:
b=strtol(*endptr,endptr,0);
^^^^^^^
Dereference a NULL leads to a crash
你的問題是一樣的,因爲這代碼:
char** endptr=NULL;
char* p = *endptr; // Crash!!
所以你的問題真有無關strtol
。
關於strtol
:
如果你想strtol
更新*endptr
,你需要傳遞一個值,該值不NULL
。
做到這一點的方式,是使char*
變量(注:不一個char**
),並通過的*地址**這char*
到strtol
。
像:
char a[]=" 0xa this is a343 good";
char* p; // Notice: just one * as you need a pointer to char
long int b=0;
b=strtol(a, &p,0);
^^
Notice: & (aka address of). So you pass the address of
a pointer to char. Equivalent to char** as expected
by strtol
你不能解引用指針爲NULL,你這樣做:'* endptr'。 – ForceBru
@BLUEPIXY我知道,但爲什麼它不適用於上面的代碼? – Sabrina
@Sabrina'* endptr'與'* NULL'相同,它給你一個分段錯誤。 – 4386427