简体   繁体   English

C表初始化

[英]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

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. z[2]处的这两个反斜杠将被视为一个反斜杠,而代码中只有一个反斜杠会使所有内容混乱。

You also don't really need to specify the array length as it'll be auto-calculated for you by the compiler. 您实际上也不需要指定数组长度,因为它将由编译器自动为您计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM