I try to figure this stuff out for myself, but I've been spending the last 3 fucking hours on this. According to stack overflow my shit is correct, so hopefully ya'll can shed some light.
I'm trying to compare a single value in a string to a char. I'm doing this like if (instr[i]== "b") However, it keeps passing everything no matter what. I've tried everything at this point, even moving it into a string and comparing those, but that just causes it to fail everytime.
Seriously, is there anything I'm missing here? Am I just being retarded?
Tried &instr[i] and *instr[i] to see if it was a pointer issue, nothing works
Jose Moore
Why is char character in double quotes? That makes it a C string and C will convert that to a pointer address during the comparison, i think. You need single quotes around the b instead of double.
Dylan Kelly
This. There's a hidden null terminator that's included.
William Perez
You've got to be kidding me! Can't believe I missed that, I just use double quotes out of habit. Fml
Thank you all for the help
Brayden Butler
Replace "b" with code]'b' That might work. Single quotes tell C you're specifying a char.
Ian Mitchell
>There's a hidden null terminator that's included. ehhhhh
I don't think this is what is happening. I think the value of a character in a string is being compared to the address of literal "b". Can we see the full function, OP?
Grayson Butler
It actually was just that, a stupid null terminator. Originally I thought I had screwed up the for loop somehow, but it was just a char classified as a string the whole time.
Juan Moore
When you compare it to a char you need to use single quotes, as user said. Chars always use single quotes.
if (instr[5] == 'c')
Carter Jones
You shouldn't believe everything you hear. The cause of this is double quotes, but it's not because of null terminator, but because you are comparing a char (on the left) to an address in memory (4 or 8 bytes depending on arch) where the string "b" is regardless of whether it has null terminator or not.