C表初始化

时间:2015-05-22 11:37:44

标签: c arrays compiler-warnings

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

 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

1 个答案:

答案 0 :(得分:3)

您可以尝试转义反斜杠

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

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

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