当我尝试在命令行上将一个数字传入我的应用程序时,在下面的代码中出现奇怪的分段错误。isdigit()分段错误
int offset = 3;
int main(int argc, char *argv[]) {
// Check for arguments to see whether there is a custom offset
if (argc == 2) {
// If argc == 2 then we have a offset?
if (isdigit((unsigned char)*argv[1])) {
offset = atoi(*argv[1]);
printf("Offset changed to: %d\n", offset);
} else {
printf("Offset not changed due to %s not being a number.\n", *argv[1]);
}
} else if(argc >= 2) {
// If argc >= 2 then we have too many arguments
printf("Too many arguments.");
return 0;
}
}
我首先在'* argv [1]'中使用圆括号。它定义了一个独特的操作序列,但我不相信自己知道哪一个。 – 2010-10-16 21:24:53
为什么'(unsigned char)'为类型'char'强制转换为'int'并将其传递给'isdigit(int)'? – msw 2010-10-16 21:25:36
我在网上阅读了一个教程,需要将其转换为(无符号字符)...无论使用什么变体,我都会得到分段错误... – Aran 2010-10-16 21:27:13