user8940323
user8940323

Reputation:

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. 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. Thanks for any help

Upvotes: 0

Views: 306

Answers (1)

user2736738
user2736738

Reputation: 30936

Don't be misquided by the similar syntax of file[0] = "test"; and char arry[20]="test";

First is assigning a decayed pointer to the string literal to a char[20] object which is type mismatch error.

Second is declaration of char array with initialization of the content with that of the string literal. This is different from the one shown above.

You can do a quick strcpy(file[0],"text") which would work. But then again use strcmp and friends to compare these two strings.

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

Upvotes: 1

Related Questions