C程序 - 警告:字符数组的初始化字符串太长

时间:2016-11-10 10:17:24

标签: c arrays

因为我是C编程的新手,请耐心等待,所以这是我的代码:

#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
    char name[0] = "/tmp/crt/client.conf";
    char name[1] = "/tmp/crt/haha";
    char name[2] = "/tmp/crt/Router.ovpn";
    char name[3] = "/tmp/crt/Router.txt";
    char name[4] = "/tmp/crt/sgdo11-tcpvpn.com-443.ovpn";
    char name[5] = "/tmp/crt/sgdo1-tcpvpn.com-443.ovpn";
    char name[6] = "/tmp/crt/sshdropbear.net-2016-10-22.ovpn";
    char name[7] = "/tmp/crt/sshdropbear.net.txt";
    char name[8] = "/tmp/crt/tcpvpn.txt";
    char name[9] = "/tmp/crt/test15.ovpn";
    char name[10] = "/tmp/crt/test15.txt";
    char name[11] = "/tmp/crt/UDP-1197.ovpn";
    char name[12] = "/tmp/crt/udp.group-1194-exp11nov.ovpn";
    char name[13] = "/tmp/crt/udp.group-1194.txt";

    int def_size[0] = 5874; // FILE: /tmp/crt/client.conf
    int def_size[1] = 1; // FILE: /tmp/crt/haha
    int def_size[2] = 2091; // FILE: /tmp/crt/Router.ovpn
    int def_size[3] = 15; // FILE: /tmp/crt/Router.txt
    int def_size[4] = 5694; // FILE: /tmp/crt/sgdo11-tcpvpn.com-443.ovpn
    int def_size[5] = 2092; // FILE: /tmp/crt/sgdo1-tcpvpn.com-443.ovpn
    int def_size[6] = 1779; // FILE: /tmp/crt/sshdropbear.net-2016-10-22.ovpn
    int def_size[7] = 36; // FILE: /tmp/crt/sshdropbear.net.txt
    int def_size[8] = 33; // FILE: /tmp/crt/tcpvpn.txt
    int def_size[9] = 5855; // FILE: /tmp/crt/test15.ovpn
    int def_size[10] = 26; // FILE: /tmp/crt/test15.txt
    int def_size[11] = 4983; // FILE: /tmp/crt/UDP-1197.ovpn
    int def_size[12] = 1930; // FILE: /tmp/crt/udp.group-1194-exp11nov.ovpn
    int def_size[13] = 31; // FILE: /tmp/crt/udp.group-1194.txt
    for(int i = 0; i < 14; i++) {
        struct stat st;
        stat(name[i], &st);
        int now = st.st_size;
        printf("VERIFYING: %s DEF_SIZE: %d NOW: %d\n", name[i], def_size[i], now);
        if (now != atoi(def_size[i])) {
            printf("(%s) has a different file size from the default one.\n", name[i]);
        }
    }


    return 0;
}

当我编译它时,我得到:

test.c: In function ‘main’:
test.c:26:17: warning: initializer-string for array of chars is too long
  char name[0] = "/tmp/crt/client.conf";
                 ^~~~~~~~~~~~~~~~~~~~~~
test.c:27:7: error: conflicting types for ‘name’
  char name[1] = "/tmp/crt/haha";
       ^~~~
test.c:26:7: note: previous definition of ‘name’ was here
  char name[0] = "/tmp/crt/client.conf";
       ^~~~
test.c:27:17: warning: initializer-string for array of chars is too long
  char name[1] = "/tmp/crt/haha";

我想要实现的是验证该数组中的每个文件,如果它与默认大小不匹配,那么显示printf我该如何修复它?

2 个答案:

答案 0 :(得分:7)

此:

char name[0] = "/tmp/crt/client.conf";

表示“make name是一个包含0个字符的数组,初始化此字符串为20个字符”。依此类推,这就是编译器警告的原因。

你的意思是:

const char *names[] = { "/tmp/crt/client.conf", ... };

这意味着“make names是一个指向非变化字符数据的指针数组,初始化为指向这些以0结尾的字符串”。后一个数组可以迭代,如你所愿:

for(size_t i = 0; i < sizeof names / sizeof *names; ++i)
{
  printf("name %zu is '%s'\n", i, names[i]);
}

但更好的方法是将名称和大小存储在一起:

const struct {
  const char *name;
  size_t def_size;
} files[] = {
 { "/tmp/crt/client.conf", 5874 },
 { "/tmp/crt/haha", 1 },
 ... and so on ...
};

这会使stat() - 循环更易于编写:

for(size_t i = 0; i < sizeof files / sizeof *files; ++i) {
    struct stat st;
    if(stat(files[i].name, &st) == 0) {
      printf("VERIFYING: %s DEF_SIZE: %zu NOW: %zu\n", files[i].name, files[i].def_size, (size_t) st.st_size);
      if (st.st_size != files[i].def_size)
        printf("(%s) has a different file size from the default one.\n", files[i].name);
    }
}

答案 1 :(得分:0)

将其更改为:

char* name[] =
{
    "/tmp/crt/client.conf",
    "/tmp/crt/haha",
    "/tmp/crt/Router.ovpn",
    "/tmp/crt/Router.txt",
    "/tmp/crt/sgdo11-tcpvpn.com-443.ovpn",
    "/tmp/crt/sgdo1-tcpvpn.com-443.ovpn",
    "/tmp/crt/sshdropbear.net-2016-10-22.ovpn",
    "/tmp/crt/sshdropbear.net.txt",
    "/tmp/crt/tcpvpn.txt",
    "/tmp/crt/test15.ovpn",
    "/tmp/crt/test15.txt",
    "/tmp/crt/UDP-1197.ovpn",
    "/tmp/crt/udp.group-1194-exp11nov.ovpn",
    "/tmp/crt/udp.group-1194.txt",
};

int def_size[] =
{
    5874, // FILE: /tmp/crt/client.conf
       1, // FILE: /tmp/crt/haha
    2091, // FILE: /tmp/crt/Router.ovpn
      15, // FILE: /tmp/crt/Router.txt
    5694, // FILE: /tmp/crt/sgdo11-tcpvpn.com-443.ovpn
    2092, // FILE: /tmp/crt/sgdo1-tcpvpn.com-443.ovpn
    1779, // FILE: /tmp/crt/sshdropbear.net-2016-10-22.ovpn
      36, // FILE: /tmp/crt/sshdropbear.net.txt
      33, // FILE: /tmp/crt/tcpvpn.txt
    5855, // FILE: /tmp/crt/test15.ovpn
      26, // FILE: /tmp/crt/test15.txt
    4983, // FILE: /tmp/crt/UDP-1197.ovpn
    1930, // FILE: /tmp/crt/udp.group-1194-exp11nov.ovpn
      31, // FILE: /tmp/crt/udp.group-1194.txt
};