char comparison error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chazzy69
    New Member
    • Sep 2007
    • 196

    char comparison error

    Hi, im just trying to compare a character(char) to a set list of characters (e.g. "a", "b", etc).

    first i read in the value- (i have tried this two ways)
    Code:
    scanf("%c", &var_name);
    //also tried this and yes i know the difference
    scanf("%s", &var_name);
    When i printf this value -
    Code:
    printf("%c this is var_name", var_name);
    It outputs what it is supposed to,

    Now for the actual error (of which there are a couple)

    When i compare var_name to another character
    e.g.
    Code:
    char var_name;
    if (var_name != "z") {
    // code goes here
    }
    I get the following error cannot compare a const char to a char.

    Then i read somewhere about adding an * to fix this problem (i thought this was a pointer by the way) -

    Code:
    char *var_name;
    if (var_name != "z") {
    // code goes here
    }
    And believe it or not it actually worked???, anyhow now its stopped working completely (e.g even though var_name = "z" it wont pick it up)

    So my question to anyone who will help how do i compare a char to a character and have it work.

    Any help in this matter is greatly appreciated, you have my thanks.
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    On the code that involves replacing %c with %s (at the beginning):
    Interesting. I thought you would have removed the ampersand from var_name, because you are passing in an array, right?

    But on to your actual problem. Just as a note, when something doesn't work, don't guess the code. You'll typically make things worse (as you did here).

    First, I'll note the problems in your two code snippets involving character comparisons. In the first snippet, the issue is using "z" instead of 'z'. Yes, the quote type matters. "z" creates a C string. 'z' creates a character.

    The error message is a bit cryptic if you are a beginner in C. So, var_name is a char. That's simple. Now look at "z". First, it's a constant. It's a constant as much as if you had typed the number 5 in place of it. Second, a C string can be pointed to with a char*. You have a constant string literal, so you have a const char*. And you try to compare that to a char. Hence the compilation error. You actually wanted the char 'z'. So use the single quotes.

    ---

    But your second snippet is even worse. So you added a * to var_name, and now you have a character pointer. This gives you an immediate problem. Suddenly, you no longer have a character. So what you have to do is allocate memory for a character, and then point var_name to it. I'm betting you omitted this portion in your code.

    Of course, the var_name != "z" comparison will pass. You are comparing two char pointers. It's always going to return false though. "z" occupies a certain place in memory. var_name isn't going to point to it. That is what comparing two pointers does, by the way. It compares if the pointer values are equal. So when you have

    Code:
    char *cstr1, *cstr2;
    ... cstr1 == cstr2 ...
    That comparison doesn't check if the string contents match. You need to explicitly go through the string contents and check if each individual character matches up with one another. That's why there is a function in the library strcmp and strncmp to do so for you.

    But you never wanted a string comparison anyway. Change "z" to 'z' and you're set to go.

    Comment

    • chazzy69
      New Member
      • Sep 2007
      • 196

      #3
      Thanks for the help, it is greatly appreciated

      Comment

      Working...