Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Understanding string comparison in C

Ostatnio zmodyfikowano 2017-05-30 18:18
Autor Wiadomość
cnp
Temat założony przez niniejszego użytkownika
Understanding string comparison in C
» 2017-05-30 04:27:55
Hi,

I am trying to understand C strings. But got puzzled with string comparison.

I have declared three string like below.
    char *str1="String1";
    char *str2="String1";
    char str3[] = "String1";

Comparision of str1==str2 is true whereas str1==str3 is false. I can't understand why so?
Can anybody help?
Full runnable code is available at http://onlinegdb.com/fork/B1mPNLqbZ

Thanks in advance.
P-161779
karambaHZP
» 2017-05-30 05:27:42
P-161780
Rashmistrz
» 2017-05-30 05:58:06
@karambaHZP:
It doesn't answers his question.
He is about "why", not about "how".

@cnp:
I don't know why in 1st case it's true...
For me all of these are pointers and they should be different. :/
(Maybe str1 and str2 are getting same pointer to the same literal?)
(str1's and str2's types are const char* tho)
P-161781
j23
» 2017-05-30 10:00:25
str1 and str2 are getting same pointer to the same literal
This.
P-161784
DejaVu
» 2017-05-30 10:39:53
str1 and str2 are getting same pointer to the same literal
Yep. And you are comparing pointers instead of string values.
P-161786
cnp
Temat założony przez niniejszego użytkownika
» 2017-05-30 15:50:29
I understand somewhat, str1 and str2 are getting same pointer to literal. But why "str3" getting different pointer to literal then? Is it just dependent on compiler?
P-161795
1aam2am1
» 2017-05-30 16:09:59
str3 is not pointer

PS.
is array
You can change the content of str3
P-161796
Saran
» 2017-05-30 16:37:22
In second example you comparise something like 0011FF74 == "String1"
P-161797
« 1 » 2
  Strona 1 z 2 Następna strona