1

Having this code:

char a[20]="wasd", b[20]="asd";
if((a+1)==b)
    printf("yes");

Will not return "yes", even if "a+1" is "asd". I am wondering what am I doing wrong?

1
  • 2
    If this is supposed to be C++ code then you would be better off using proper C++ strings (std::string) rather than old skool C char *s - that way you can just use == to test for equality rather than calling C library functions such as strcmp. Commented Mar 5, 2015 at 10:58

5 Answers 5

3

You need to use strcmp to compare C strings. == will just compare the pointers.

For example:

#include <string.h> // or <cstring> if you're writing C++
...
char a[20]="wasd", b[20]="asd";
if(strcmp(a+1, b)==0)
    printf("yes");

By the way, if you're writing C++, you'd be better off using std::string. Then you could have simply used == to compare them.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, I don't know why I did not use strcmp from the beginning.
No problem :) btw if you're writing C++, you'd be better of using std::string. Then you could have simply used == to compare them.
Hi @zenith, I think that last comment is important enough to be in the answer, so I took the liberty of editing it in. Feel free to change it if you disagree.
@CompuChip Thanks, I was actually thinking the same.
2

If it's not a student assignment and you truly are using C++(as your tag says) you should use strings. Now you're using arrays and comparing arrays addresses instead of real strings. In a C++ way your code might look like:

#include <iostream>
#include <string>

int main()
{
    std::string a ="wasd";
    std::string b ="asd";
    if(a.substr(1) == b)
        std::cout << "Yes!\n";
}

Well, there is a better way to find if one string contains another but the code is a direct mapping of your C code to the C++-ish one.

Comments

1

You are actually comparing pointer addresses, not the actual string contents.

Your code should use strcmp:

char a[20]="wasd", b[20]="asd";
if(strcmp(a+1, b) == 0)
    printf("yes");

Be careful that strcmp returns 0 if the strings are identical.

A better and more idiomatic alternative would be to use std::string:

std::string a = "wasd", b = "asd";
if(a.substr(1) == b)
    std::cout << "yes";

substr does copy the string though, so it is slightly less efficient than the previous approach.

Comments

0

You have to use strcmp from string.h to compare strings. if(strcmp(a+1,b)==0) in Your case.

Comments

0

As per your code, when using (a+1)==b you are comparing the addresses of the pointers pointing respectively to second character of string 'a' and the first character of string 'b'.

It can work if you modify your code as:

char a[20]="wasd", b[20]="asd";
if(*(a+1)==*b)         // now we are comparing the values towards which the
    printf("yes");     // respective pointers are pointing

You can also use compare() for comparison of strings included in .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.