当我试图在命令行上将一个数字传递到我的应用程序时,在下面的代码中出现了一个奇怪的分段错误。
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
", offset);
} else {
printf("Offset not changed due to %s not being a number.
", *argv[1]);
}
} else if(argc >= 2) {
// If argc >= 2 then we have too many arguments
printf("Too many arguments.");
return 0;
}
}