當我嘗試在命令行上將一個數字傳入我的應用程序時,在下面的代碼中出現奇怪的分段錯誤。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