简体   繁体   English

C-比较并将char *转换为char

[英]C - comparison and cast char* to char

I have the following code: 我有以下代码:

char* line[255];
....
line[0] = '1'; // filled char by char
....

char charCode = (char)line[i];
if (charCode == '\t' || charCode == '\n' || charCode == ',' || charCode == ' ')
{
      // do someting
}

The IF condition I'm using to find space, comma, tab, or end of line. 我用于查找空格,逗号,制表符或行尾的IF条件。 The warning I get from GCC: 我从海湾合作委员会得到的警告:

   warning: cast from pointer to integer of different size

Can someone help me spot the issue here? 有人可以帮我在这里找到问题吗? Thanks, 谢谢,

Change: 更改:

line[0] = "1"; // filled char by char

to this: 对此:

line[0] = '1';

Also, change: 另外,更改:

char* line[255];

to: 至:

char line[255];

In C, string literals in double quotes " are of type char * , while literals enclosed in single quotes ' are character literals with type char . Also, you have declared an array of pointers to characters (or an array of strings, essentially) where I think you just wanted an array that can hold 255 characters. 在C中,在双引号字符串文字"是类型的char * ,而在单引号文字'是字符文字与型char 。此外,您已声明指针数组以字符(或字符串数组,基本上),其中我想您只是想要一个可以容纳255个字符的数组。

It's too hard to do this in comments. 在评论中很难做到这一点。 The code should look like this. 该代码应如下所示。

char line[255];
....
line[0] = '1'; // filled char by char
....

char charCode = line[i];
if (charCode == '\t' || charCode == '\n' || charCode == ',' || charCode == ' ')
{
      // do someting
}

The array should be char, not char*. 数组应该是char,而不是char *。 That fixes the error. 可以修复错误。 Then the cast is not needed. 那么就不需要强制转换了。

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

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