+ 2

How this code answer is true??

Here both pointer is pointing different string and how if condition is true?? #include<stdio.h> Main() { Char *p="abc",*q="abc"; If(p==q) Printf("same"); Else Printf("not same"); }

24th Mar 2020, 3:48 PM
tom
tom - avatar
2 Respostas
+ 3
As Ipang already said, you can not rely on this behaviour because it is an optimization technique applied by the compiler that is not demanded by the language standard. Have a look at the following thread: https://stackoverflow.com/questions/690176/c-c-optimization-of-pointers-to-string-constants
24th Mar 2020, 4:40 PM
Shadow
Shadow - avatar
+ 4
In SoloLearn this outputs "same", but in my phone it outputs "not same" (using C4Droid). Personally, I wouldn't count on or expect certainty from this code, seeing how output differs by compiler.
24th Mar 2020, 4:17 PM
Ipang