简体   繁体   English

二维数组和字符数组C的比较

[英]Comparison of 2D array and character array C

I am comparing data stored in a 2D array that I read from a file to a character array with data in it. 我正在比较从文件中读取的2D数组中存储的数据和其中包含数据的字符数组。 It looks like this: 看起来像这样:

char file[10][20];
file[0] = "test";
char arry[20] = "test";
if (arry == file[0]) {
    //do something
}
else {
    // do something else
}

The issue is that the //do something statement is not being executed, I have made sure there are no new line characters that were left on when the file was read into the array. 问题是// do something语句没有被执行,我确保在将文件读入数组时没有换行符。 Thanks for any help 谢谢你的帮助

Don't be misquided by the similar syntax of file[0] = "test"; 不要被file[0] = "test";的相似语法所迷惑file[0] = "test"; and char arry[20]="test"; char arry[20]="test";

First is assigning a decayed pointer to the string literal to a char[20] object which is type mismatch error. 首先是将一个衰减的指向字符串文字的指针分配给char[20]对象,该对象是类型不匹配错误。

Second is declaration of char array with initialization of the content with that of the string literal. 第二个是char数组的声明,其中用字符串文字初始化内容。 This is different from the one shown above. 这与上面显示的不同。

You can do a quick strcpy(file[0],"text") which would work. 您可以执行一个快速的strcpy(file[0],"text") But then again use strcmp and friends to compare these two strings. 但是然后再次使用strcmp和朋友来比较这两个字符串。

if(strcmp(file[0],arry) == 0){
   ...
}

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

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