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

hil*_*llz 0 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;
}
Run Code Online (Sandbox Code Playgroud)

当我编译它时,我得到:

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";
Run Code Online (Sandbox Code Playgroud)

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

unw*_*ind 7

这个:

char name[0] = "/tmp/crt/client.conf";
Run Code Online (Sandbox Code Playgroud)

表示"make name为0个字符的数组,初始化此字符串为20个字符".依此类推,这就是编译器发出警告的原因.

你的意思是:

const char *names[] = { "/tmp/crt/client.conf", ... };
Run Code Online (Sandbox Code Playgroud)

这意味着"make names是一个指向非变化字符数据的指针数组,初始化为指向这些以0结尾的字符串".后一个数组可以像你想要的那样迭代:

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

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

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

这将使stat()-loop更容易编写:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)