Why does “==” return true for character pointers? [duplicate]












2















This question already has an answer here:




  • Two string literals have the same pointer value?

    5 answers



  • String Literal address across translation units [duplicate]

    2 answers




When we compare strings in C, we are careful to use strcmp (or its other variants) to do equality checks. For example, if one string is char hello1[7] = "hello!" and another string is char hello2[7] = "hello!", we can check if their contents are equal using strcmp. However, we cannot use == since == will compare the address of the first element of each array (due to array decay), and that is always false.



So why is it that when I try to compare two char * with ==, the result is true? For example:



int main() {
char *str1 = "Hello";
char *str2 = "Hello";

if (str1 == str2) {
printf("equaln");
} else {
printf("not equaln");
}
}


This will print equal. Based on my understanding, a pointer is essentially an address, so a char * is an address of a location containing a character. So how can two addresses be the same here?










share|improve this question













marked as duplicate by par, umop apisdn, Daniel H, o11c, phuclv Nov 21 at 4:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • see String Literal address across translation units
    – Shafik Yaghmour
    Nov 21 at 3:36












  • String literals are often "merged" so that there's only one copy of any given string. Check the pointer values.
    – Retired Ninja
    Nov 21 at 3:36










  • Thanks for the rapid responses! Somehow when I searched I could not find these posts, but they are very helpful!
    – umop apisdn
    Nov 21 at 3:37
















2















This question already has an answer here:




  • Two string literals have the same pointer value?

    5 answers



  • String Literal address across translation units [duplicate]

    2 answers




When we compare strings in C, we are careful to use strcmp (or its other variants) to do equality checks. For example, if one string is char hello1[7] = "hello!" and another string is char hello2[7] = "hello!", we can check if their contents are equal using strcmp. However, we cannot use == since == will compare the address of the first element of each array (due to array decay), and that is always false.



So why is it that when I try to compare two char * with ==, the result is true? For example:



int main() {
char *str1 = "Hello";
char *str2 = "Hello";

if (str1 == str2) {
printf("equaln");
} else {
printf("not equaln");
}
}


This will print equal. Based on my understanding, a pointer is essentially an address, so a char * is an address of a location containing a character. So how can two addresses be the same here?










share|improve this question













marked as duplicate by par, umop apisdn, Daniel H, o11c, phuclv Nov 21 at 4:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • see String Literal address across translation units
    – Shafik Yaghmour
    Nov 21 at 3:36












  • String literals are often "merged" so that there's only one copy of any given string. Check the pointer values.
    – Retired Ninja
    Nov 21 at 3:36










  • Thanks for the rapid responses! Somehow when I searched I could not find these posts, but they are very helpful!
    – umop apisdn
    Nov 21 at 3:37














2












2








2








This question already has an answer here:




  • Two string literals have the same pointer value?

    5 answers



  • String Literal address across translation units [duplicate]

    2 answers




When we compare strings in C, we are careful to use strcmp (or its other variants) to do equality checks. For example, if one string is char hello1[7] = "hello!" and another string is char hello2[7] = "hello!", we can check if their contents are equal using strcmp. However, we cannot use == since == will compare the address of the first element of each array (due to array decay), and that is always false.



So why is it that when I try to compare two char * with ==, the result is true? For example:



int main() {
char *str1 = "Hello";
char *str2 = "Hello";

if (str1 == str2) {
printf("equaln");
} else {
printf("not equaln");
}
}


This will print equal. Based on my understanding, a pointer is essentially an address, so a char * is an address of a location containing a character. So how can two addresses be the same here?










share|improve this question














This question already has an answer here:




  • Two string literals have the same pointer value?

    5 answers



  • String Literal address across translation units [duplicate]

    2 answers




When we compare strings in C, we are careful to use strcmp (or its other variants) to do equality checks. For example, if one string is char hello1[7] = "hello!" and another string is char hello2[7] = "hello!", we can check if their contents are equal using strcmp. However, we cannot use == since == will compare the address of the first element of each array (due to array decay), and that is always false.



So why is it that when I try to compare two char * with ==, the result is true? For example:



int main() {
char *str1 = "Hello";
char *str2 = "Hello";

if (str1 == str2) {
printf("equaln");
} else {
printf("not equaln");
}
}


This will print equal. Based on my understanding, a pointer is essentially an address, so a char * is an address of a location containing a character. So how can two addresses be the same here?





This question already has an answer here:




  • Two string literals have the same pointer value?

    5 answers



  • String Literal address across translation units [duplicate]

    2 answers








c string






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 3:32









umop apisdn

342513




342513




marked as duplicate by par, umop apisdn, Daniel H, o11c, phuclv Nov 21 at 4:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by par, umop apisdn, Daniel H, o11c, phuclv Nov 21 at 4:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • see String Literal address across translation units
    – Shafik Yaghmour
    Nov 21 at 3:36












  • String literals are often "merged" so that there's only one copy of any given string. Check the pointer values.
    – Retired Ninja
    Nov 21 at 3:36










  • Thanks for the rapid responses! Somehow when I searched I could not find these posts, but they are very helpful!
    – umop apisdn
    Nov 21 at 3:37


















  • see String Literal address across translation units
    – Shafik Yaghmour
    Nov 21 at 3:36












  • String literals are often "merged" so that there's only one copy of any given string. Check the pointer values.
    – Retired Ninja
    Nov 21 at 3:36










  • Thanks for the rapid responses! Somehow when I searched I could not find these posts, but they are very helpful!
    – umop apisdn
    Nov 21 at 3:37
















see String Literal address across translation units
– Shafik Yaghmour
Nov 21 at 3:36






see String Literal address across translation units
– Shafik Yaghmour
Nov 21 at 3:36














String literals are often "merged" so that there's only one copy of any given string. Check the pointer values.
– Retired Ninja
Nov 21 at 3:36




String literals are often "merged" so that there's only one copy of any given string. Check the pointer values.
– Retired Ninja
Nov 21 at 3:36












Thanks for the rapid responses! Somehow when I searched I could not find these posts, but they are very helpful!
– umop apisdn
Nov 21 at 3:37




Thanks for the rapid responses! Somehow when I searched I could not find these posts, but they are very helpful!
– umop apisdn
Nov 21 at 3:37












1 Answer
1






active

oldest

votes


















4














Because the two addresses are the same. Your compiler included one copy of the string "Hello" in your program and made str1 and str2 both point to it.



The C standard specifies that string literals might or might not be distinct arrays in memory, and that undefined things might happen if you modify them in order to allow the compiler to do exactly this.






share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    Because the two addresses are the same. Your compiler included one copy of the string "Hello" in your program and made str1 and str2 both point to it.



    The C standard specifies that string literals might or might not be distinct arrays in memory, and that undefined things might happen if you modify them in order to allow the compiler to do exactly this.






    share|improve this answer


























      4














      Because the two addresses are the same. Your compiler included one copy of the string "Hello" in your program and made str1 and str2 both point to it.



      The C standard specifies that string literals might or might not be distinct arrays in memory, and that undefined things might happen if you modify them in order to allow the compiler to do exactly this.






      share|improve this answer
























        4












        4








        4






        Because the two addresses are the same. Your compiler included one copy of the string "Hello" in your program and made str1 and str2 both point to it.



        The C standard specifies that string literals might or might not be distinct arrays in memory, and that undefined things might happen if you modify them in order to allow the compiler to do exactly this.






        share|improve this answer












        Because the two addresses are the same. Your compiler included one copy of the string "Hello" in your program and made str1 and str2 both point to it.



        The C standard specifies that string literals might or might not be distinct arrays in memory, and that undefined things might happen if you modify them in order to allow the compiler to do exactly this.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 at 3:38









        hobbs

        140k14147232




        140k14147232















            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'