在extract_last_integer()
你里面
while (*p) { // While there are more characters to process... if ( isdigit(*p) || ( (*p=='-'||*p=='+') && isdigit(*(p+1)) )) { // Found a number val = strtol(p, &p, 10); // Read number } else { // Otherwise, move on to the next character. p++; } }
递增,p
直到遇到第一个数字或-
/ +
后跟一个数字。因此它将与第一个10
数字匹配。不过请注意,您并没有像早return val;
于那样打破循环extract_first_integer()
。当您继续匹配下一位数字时-6
,"10-6"
将匹配输入。而且10 - (-6)
显然是16
您还可能具有未定义的行为
将相同的指针传递到strtol
。该变量str
未使用,应str_end
改为在参数中传递
将const char*
("10-6"
)传递给期望的函数char*