书中的命令行参数程序不起作用...(C)

标签 c pointers command-line-arguments codeblocks

在第 13 章快结束时,一个程序演示了应该打印的命令行参数

Jupiter is planet 5
venus is not a planet
Earth is planet 3
fred is not a planet

输入

planet Jupiter venus Earth fred

我用我的代码检查了章节代码,一切都检查过了,我正在使用 Code:Blocks 并编译为 C99。

当我运行程序时,程序立即结束,要求按任意键退出。

    // Checks planet names

    #include <stdio.h>
    #include <string.h>

    #define NUM_PLANETS 9

    int main(int argc, char *argv[])
    {
        char *planets[] = {"Mercury", "Venus", "Earth",
                           "Mars", "Jupiter", "Saturn",
                           "Uranus", "Neptune", "Pluto"};
        int i, j;

        for (i = 1; i < argc; i++) {
            for (j = 0; j < NUM_PLANETS; j++)
                if (strcmp(argv[i], planets[j]) == 0) {
                    printf("%s is planet %d\n", argv[i], j + 1);
                    break;
                }
            if (j == NUM_PLANETS)
                printf("%s is not a planet\n", argv[i]);
        }

        return 0;
    }

最佳答案

您需要提供命令行参数。

使用 CodeBlocks 你可以这样设置:

Project > Set programs' arguments...

这将打开一个窗口,您可以在其中插入参数。在这里输入:

Jupiter venus Earth fred

关于书中的命令行参数程序不起作用...(C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23718839/

相关文章:

c++ - 数组索引和地址返回相同的值

计算通信计算机网络中受恶意软件感染的计算机组?

c - 声明 '(int *) ptr[N]' 在 C 中是什么意思?

c - C语言 : pointer to integer conversion assigning to 'char' from 'char [2]'

c - 使用 argv[] 构建一个字符转换器程序

perl - ImageMagick命令行选项顺序(以及命令行参数的类别)

objective-c - 苹果操作系统 X : Getting detailed process information (specifically its launch arguments) for arbitrary running applications using its PID

c - 获取单个字符并将其传递给 C 中的函数

无法从 'int *' 转换为 'int'

command-line - 命令行参数存储在哪里?