C pointers
27-Nov-92 16:42:55
Sb: #30393-C pointers
Fm: Greg Comeau@Comeau Cmptg 72331,3421
To: Steven Park 73617,3462
>How do you make a char pointer comparison? If you go …. char *name = "Bob";
>if(name == "Bob") printf("Hello %s. \n",name);
>you'll never see `Bob' printed.
And just to add to the comments already offered. Note that the string
literal in the if statement is an unnamed region of storage. Its utterance
in your if statment results in the *address* of where the B is.
This makes sense if you look at how the "Bob" in 'char *name = "Bob";'
works. name now points at the B in that Bob.
So, name == "Bob" is comparing addresses and not string, and it is the
latter which I think that you want to do.
To make this more complicated, your C implementation actually has the liberty
to put the two literals into the same storage and so the possibility did
exist that it would have printed the Hello… string. But that is not the
way you intended the result and hence you should stay away from that
approach and use the strcmp as was suggested.