【问题标题】:Linux Shell in C - warning: excess elements in struct initializerC 中的 Linux Shell - 警告:结构初始化程序中的多余元素
【发布时间】:2021-02-27 17:02:13
【问题描述】:

我正在尝试用 C 语言制作一个 Linux shell,但是当我编译它时说:

18710.c:13:20: warning: excess elements in struct initializer
     {"who", "who", "show who is logged on"},
                    ^~~~~~~~~~~~~~~~~~~~~~~
18710.c:13:20: note: (near initialization for ‘list[0]’)
18710.c:14:20: warning: excess elements in struct initializer
     {"list", "ls", "list directory contents"},
                    ^~~~~~~~~~~~~~~~~~~~~~~~~
18710.c:14:20: note: (near initialization for ‘list[1]’)
18710.c:15:23: warning: excess elements in struct initializer
     {"manual", "man", "an interface to the system reference manuals"},
                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18710.c:15:23: note: (near initialization for ‘list[2]’)
18710.c:16:26: warning: excess elements in struct initializer
     {"processes", "top", "display processes"},
                          ^~~~~~~~~~~~~~~~~~~
18710.c:16:26: note: (near initialization for ‘list[3]’)
18710.c:17:24: warning: excess elements in struct initializer
     {"clear", "clear", "clear the terminal screen"},
                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~
18710.c:17:24: note: (near initialization for ‘list[4]’)
18710.c:18:22: warning: excess elements in struct initializer
     {"exit", "exit", "close the terminal screen"}
                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~
18710.c:18:22: note: (near initialization for ‘list[5]’)

这是我的结构;

typedef struct command{
    char *cmd;
    char *path;
}CMD;

CMD list[]={
    {"who", "who", "show who is logged on"},
    {"list", "ls", "list directory contents"},
    {"manual", "man", "an interface to the system reference manuals"},
    {"processes", "top", "display processes"},
    {"clear", "clear", "clear the terminal screen"},
    {"exit", "exit", "close the terminal screen"}
};

我知道这只是一个警告,但我还有其他方法可以解决它吗?我不能有第三个参数吗? 非常感谢

【问题讨论】:

  • 您的结构有两个成员,但您提供了 三个 字符串来初始化每个结构。最后一个应该初始化的字符串是什么结构成员?
  • 是的,这是一个愚蠢的错误!不好意思打扰了,谢谢!

标签: c linux shell


【解决方案1】:

您的结构只有 2 个 (*cmd, *path) 变量,而您正尝试使用 3 个变量对其进行初始化

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    相关资源
    最近更新 更多