-3

I might not be understanding my code entirely, or not understand how this works. Appreciate any comments.

I seem to be a able to compare a between 2 strings literals without dereferencing them.

I do a :

if(tail->name==name) cout << "Match" << endl;

And this seems to work. Tail->name and name are both char *string pointers, and *name is a string literal passed in by argument to the function. Should i not do a *name==*name instead?

The only other explanation is that it would compare the value the pointers points to, but then it should work like it does.

Regards, atv

EdChum
  • 376,765
  • 198
  • 813
  • 562
ATv
  • 65
  • 2
  • 11
  • 1
    You should use `strcmp` or `strncmp`. – songyuanyao Jun 08 '16 at 10:10
  • 2
    It's valid to compare pointer address values, the issue is intent here and as you've pointed out your intent is to compare the values of what the pointer points to – EdChum Jun 08 '16 at 10:11
  • 2
    If you're using C++, then why not use `std::string` which can be compared by value using the `==` operator. – MicroVirus Jun 08 '16 at 10:15

2 Answers2

1

It's absolutely correct and is a very good optimisation in some cases.

But it has a subtlety. Simply comparing the pointers will never yield a false positive but the converse is not true: if tail->name points to a different string to name then if(tail->name==name) will be false even if the contents of the strings are identical.

So, in general, you can't use this approach, but you need to use something like strcmp or, better still in many cases, std::string.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

I seem to be a able to compare a between 2 strings literals without dereferencing them.

To be more exact, you're comparing pointers to string literals, not the literals themselves.

Should i not do a *name==*name instead?

If you intend to compare only the first character, then yes, although tail->name[0] == name[0] would be more descriptive in my opinion.

If you intend to compare contents of the string, then you must dereference the pointers and iterate through each string in parallel, comparing the characters along the way. A standard function exists for that: std::strcmp.

If you instead want to compare the pointers, then what you're doing currently is correct. Do note that identical but separate string literals are not guaranteed to have the same address, so this might not work as you may intend it to. It depends on your use case, whether this approach is appropriate.

I'll try to explain with an example:

const char* argument = "test";
const char* name = argument;
const char* another_name = argument;
assert(name == another_name);

The assertion is guaranteed to be true, there should be nothing strange about it. The pointers are copies of each other and point to the same address which is where the string literal is stored.

However:

const char* name = "test";
const char* another_name = "test";
assert(name == another_name);

This assertion is not guaranteed to be true. But it is not guaranteed to be false either. It is unspecified and up to the implementation. It should not be surprising that the identical string literals could have the same address, it's a common optimisation. But you cannot rely on that. In this situation, compare the contents of the string rather than the address, as explained earlier in the answer.

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • I intend to compare the contents of the string. I'll post a relevant code snippet tonight and the outcome, but basically i am passing in a char *argument, i.e. "Lisa", assigning that to tail->name (tail->name=argument), and then doing a compare between tail->name and the passed in *argument (if(tail->name==argument). And this seems to work strangely enough. – ATv Jun 08 '16 at 11:18
  • @AlefVeld I don't find it strange. That's what I would have expected. Check out my extended answer, I hope it explains the situation well. – eerorika Jun 08 '16 at 11:52
  • Ah ok i get it. So you are saying that's a compiler optimization (or undefined) and that it's, for lack of a better word, pure luck that my strings match. I learned something new, i didn't realize the compiler might give the same pointer addresses to the same string literals. That's...almost a bit naughty of the compiler (but correct if i should use the dereferenced value instead anyway). I will use strcmp() or something similar. Thanks for your great answer! – ATv Jun 08 '16 at 12:14
  • One question though: If i dereference the value as i'm supposed to, and char * pointers for the same string literal might or might not have the same address, then how can i, or the system, compare char *name and char *name (both with the same string). Or does it not optimize/have undefined behaviour if i dereference instead? – ATv Jun 08 '16 at 12:25