分段故障iPhone可执行文件

时间:2012-06-24 00:51:35

标签: c command-line-interface segment

当我在iphone上运行CLI程序时,我遇到了Segment错误:11错误。我不知道该怎么做,我是C的菜鸟。

int main (int argc, const char * argv[])
{
if (argc > 1 && (!strcmp(argv[1],"--help") || !strcmp(argv[1],"-h"))) {

    printf("#### redhai 1.2 ####\n");
    printf("-j Jailbreak\n");
    printf("-i Device info\n");
    printf("-a About\n");
    printf("*END OF HELP*\n");
    printf("####################\n");
    return 0;
}else if (!strcmp(argv[1],"-j")) {
    printf("Coding the jailbreak portion!\n");
    return 0;
}
return 0;
}

2 个答案:

答案 0 :(得分:3)

您正在访问argv[1],而不先检查它是否存在。您需要先检查argc > 1

} else if (argc > 1 && !strcmp(argv[1],"-j")) {
//         ^^^^^^^^^^^^

答案 1 :(得分:0)

问题在于逻辑。如果NULL小于或等于strcmp,则可以将argc传递给1。要修复它,请按以下方式重新排序支票:

int main (int argc, const char * argv[])
{
    if (argc > 1) {
        if (!strcmp(argv[1],"--help") || !strcmp(argv[1],"-h")) {
            printf("#### redhai 1.2 ####\n");
            printf("-j Jailbreak\n");
            printf("-i Device info\n");
            printf("-a About\n");
            printf("*END OF HELP*\n");
            printf("####################\n");
        } else if (!strcmp(argv[1],"-j")) {
            printf("Coding the jailbreak portion!\n");
        }
    }
    return 0;
}