Muffin123 Muffin123 - 4 years ago 106
C Question

C table initialisation

I want to intialise a table that should contain a few chars for comparing it to values later, this is my code:

char z[8] = {' ', '.', '\', ':', 'o', '&', '8', '#', '@'};


I get a bunch of error messages from the compiler though, some are:



dn08.c: In function ‘main’:

dn08.c:16:37: warning: multi-character character constant [-Wmultichar]
unsigned char z[8] = {' ', '.', '\', ':', 'o', '&', '8', '#', '@'};
^
dn08.c:16:5: warning: large integer implicitly truncated to unsigned type [-Woverflow]
unsigned char z[8] = {' ', '.', '\', ':', 'o', '&', '8', '#', '@'};
^
dn08.c:16:43: error: expected ‘}’ before ‘:’ token
unsigned char z[8] = {' ', '.', '\', ':', 'o', '&', '8', '#', '@'};
^
dn08.c:16:44: warning: multi-character character constant [-Wmultichar]
unsigned char z[8] = {' ', '.', '\', ':', 'o', '&', '8', '#', '@'};
^
dn08.c:16:49: warning: multi-character character constant [-Wmultichar]
unsigned char z[8] = {' ', '.', '\', ':', 'o', '&', '8', '#', '@'};
^
dn08.c:16:54: warning: multi-character character constant [-Wmultichar]
unsigned char z[8] = {' ', '.', '\', ':', 'o', '&', '8', '#', '@'};
^
dn08.c:16:59: warning: multi-character character constant [-Wmultichar]
unsigned char z[8] = {' ', '.', '\', ':', 'o', '&', '8', '#', '@'};
^
dn08.c:16:63: error: stray ‘#’ in program
unsigned char z[8] = {' ', '.', '\', ':', 'o', '&', '8', '#', '@'};
^
dn08.c:16:64: warning: multi-character character constant [-Wmultichar]
unsigned char z[8] = {' ', '.', '\', ':', 'o', '&', '8', '#', '@'};
^
dn08.c:16:68: error: stray ‘@’ in program
unsigned char z[8] = {' ', '.', '\', ':', 'o', '&', '8', '#', '@'};
^
dn08.c:16:69: warning: missing terminating ' character
unsigned char z[8] = {' ', '.', '\', ':', 'o', '&', '8', '#', '@'};
^
dn08.c:16:43: error: missing terminating ' character
unsigned char z[8] = {' ', '.', '\', ':', 'o', '&', '8', '#', '@'};
^
dn08.c:51:1: error: expected ‘,’ or ‘;’ at end of input
}
^
dn08.c:51:1: error: expected declaration or statement at end of input

Answer Source

You can try escaping the backslash

 char z[] = {' ', '.', '\\', ':', 'o', '&', '8', '#', '@'};

These two backslashes at z[2] will be treated as one backslash while only one backslash in your code messes everything up.

You also don't really need to specify the array length as it'll be auto-calculated for you by the compiler.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download