tags:

views:

184

answers:

2

All,

I come from java and php world so this maybe a factor. But I have a problem with:

printf("%s\n",data[0]);

if(data[0] == "BG01") {
    printf("%s\n",otherstring);
}

The problem is that first printf returns in the console "BG01" but for some reason the IF condition doesn't pick up on it and the second printf never gets executed.

What's wrong with this picture?

Thanks, goe

+8  A: 

In C, you have to use strcmp(), much like you have to use .equals() in Java:

if (strcmp(data[0], "BG01") == 0) ...
Greg Hewgill
+1, but why do you accuse random people of knowing java? ;-)
Michael Krelin - hacker
The OP said "I come from java and php world" so I concluded that knowledge of `.equals()` would be a valid assumption.
Greg Hewgill
It is, I just wasn't sure how to attack this in C.'Thanks Greg, this worked great.
goe
Oops, sorry, Greg, I should've paid more attention to OP.
Michael Krelin - hacker
+12  A: 

The way you are doing it now is that you are comparing 2 pointers instead of the strings they point to. These pointers could point to the same value, but located in very different spots in memory and so be not true.

The way to do it is the use the strcmp(string1, string2) function which will check the strings themselves and not the pointers.

Toad