C Program - warning: initializer-string for array of chars is too long -


please bear me since i'm new c programming, here's code:

#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 = 0; < 14; i++) {         struct stat st;         stat(name[i], &st);         int = 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 different file size default one.\n", name[i]);         }     }       return 0; } 

when compile that, get:

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

what trying achieve verify each file in array, if doesn't match default size show printf how should fix ?

this:

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

means "make name array of 0 characters, initialized string of 20 characters". , on, why compiler warning.

you meant:

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

which means "make names array of pointers non-changing character data, initialized point @ these 0-terminated strings". latter array can iterated on wanted:

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

even better, though, store names , sizes together:

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

that make stat()-loop easier write:

for(size_t = 0; < 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 different file size default one.\n", files[i].name);     } } 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -