English 中文(简体)
isdigit()分段故障
原标题:isdigit() Segmentation Fault

当我试图在命令行上将一个数字传递到我的应用程序时,在下面的代码中出现了一个奇怪的分段错误。

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;
    }
}
最佳回答

argv[1]已经是一个字符串(类型为char*),因此在将第一个字节传递给atoi()和printf()时,写入*argv[1]会导致segfault。

将其修复为:

offset = atoi(argv[1]);

printf("Offset not changed due to %s not being a number. ", argv[1]);

问题回答

代码中真正的问题是您试图调用未声明的函数(您必须使用C89/90编译器)。您调用isdigit。您可以调用printf。您调用atoi。你对后两者的称呼不正确。编译器无法通知您这些函数被错误调用的唯一原因是您忘记了声明它们。

包括<;ctype.h><;标准库.h><;stdio.h>,以便编译器知道atoi和其他函数的正确参数类型。一旦这样做,您就应该能够找出atoi的问题,因为编译器会发出一条诊断消息来解释问题。然后您可以相应地更改呼叫。一些编译器也能够检测到printf调用的问题。

请注意,即使您按照其他答案中的建议更改atoiprintf调用(即对阿托伊(argv[1])等),您的代码仍将保持无效,因为在C89/90中,在未首先声明print f的情况下调用它会导致未定义的行为(在C99中,在不首先声明的情况下,调用任何函数完全是非法的)。

问题是打电话给atoi。它需要一个字符串。将其更改为

   offset = atoi(argv[1]);
#include <ctype.h>
int isdigit(int c);

isdigit() expects single char to check, argv[] is a pointer to string (array of chars). Long story short, it won t check strings like "1234", but it will check 1 .





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签