Ctable初始化

C table initialisation

我想初始化一个 table,它应该包含一些字符以便稍后将其与值进行比较,这是我的代码:

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

虽然我从编译器那里收到了一堆错误消息,其中一些是:

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

您可以尝试转义反斜杠

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

z[2] 处的这两个反斜杠将被视为一个反斜杠,而您的代码中只有一个反斜杠会搞砸一切。

您实际上也不需要指定数组长度,因为它会由编译器自动为您计算。