-2

i've written some simple code as an SSCCE, I'm trying to check if string entered is equal to a string i've defined in a char pointer array, so it should point to the string and give me a result. I'm not getting any warnings or errors but I'm just not getting any result (either "true" or "false")

is there something else being scanned with the scanf? a termination symbol or something? i'm just not able to get it to print out either true or false

code:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>


#define LENGTH 20

//typedef char boolean;

int main(void)
{
    const char *temp[1];
    temp[0] = "true\0";
    temp[1] = "false\0";

    char var[LENGTH];

    printf("Enter either true or false.\n");
    scanf("%s", var);


    if(var == temp[0]) //compare contents of array
    {
        printf("\ntrue\n");
    }
    else if(var == temp[1]) //compare contents of array
    {
        printf("\nfalse\n");
    }
}
prmottajr
  • 1,802
  • 1
  • 12
  • 22
shellcoder
  • 109
  • 1
  • 8
  • 3
    You don't compare C strings with `==`, but `strcmp`, for starters ... – AntonH Jan 27 '14 at 23:00
  • yea I was thinking as much, but isn't there a way to compare with == as well? I may have been misinformed from another answer here – shellcoder Jan 27 '14 at 23:02
  • No, there isn't. There are other errors, but I'm not addressing those since your question is about comparaison of strings. – AntonH Jan 27 '14 at 23:04
  • @user2387699 No, there isn't. Maybe they were confusing C with C++ where the standard string class has an `==` operator that works intuitively. –  Jan 27 '14 at 23:04
  • BTW, **you** seem to be confusing (and what's even worse, mixing) the two languages too. The code is C, not C++, it is tagged with C as well, yet you are including ``. Don't. Also, the trailing NUL character is superfluous. –  Jan 27 '14 at 23:06
  • 1
    @H2CO3 Puts 2 null terminators, to make sure the string is REALLY well terminated :P – AntonH Jan 27 '14 at 23:08
  • @AntonH and then overflows *another* buffer... (is it just me, or does this behavior remind anyone else to Bad Luck Brian?) –  Jan 27 '14 at 23:09

3 Answers3

2
const char *temp[1];

This defines tmp an array that can store 1 char* element.

temp[0] = "true\0";

Assigngs to the first element. This is okay.

temp[1] = "false\0";

Would assign to the second element, but temp can only store one. C doesn't check array boundaries for you. Also not that you don't have to specify the terminating '\0' explicitly in string literals, so just "true" and "false" are sufficient.

if(var == temp[0])

This compares only the pointer values ("where the strings are stored"), not the contents. You need the strcmp() function (and read carefully, the returned value for equal strings might not be what you expect it to be).

Brave Sir Robin
  • 1,048
  • 6
  • 9
1

Use strcmp for comparing strings:

#include <stdio.h>

int main(void)
{
    const int LENGTH = 20;
    char str[LENGTH];

    printf("Type \"true\" or \"false\:\n");
    
    if (scanf("%19s", str) != 1) {
        printf("scanf failed.");
        return -1;
    }

    if(strcmp(str, "true") == 0) {
        printf("\"true\" has been typed.\n");
    }
    else if(strcmp(str, "false") == 0) {
        printf("\"false\" has been typed.\n");
    }

    return 0;
}

and also note that:

  • string literals automatically contain null-terminating character ("true", not "true\0")
  • const int LENGTH is better than #define LENGTH since type safety comes with it
  • "%19s" ensures that no more than 19 characters (+ \0) will be stored in str
  • typedef char boolean; is not a good idea
  • unlikely, but still: scanf doesn't have to succeed
  • and there is no #include <iostream> in :)
Community
  • 1
  • 1
LihO
  • 39,598
  • 10
  • 94
  • 164
0

== checks for equality. Let's see what you're comparing.

  • The variable var is declared as a character array, so the expression var is really equivalent to &var[0] (the address of the first character in the var array).
  • Similarly, temp[0] is equivalent to &temp[0][0] (the address of the first character in the temp[0] array).

These addresses are obviously different (otherwise writing var would automatically write temp[0] as well), so == will always return 0 for your case.

strcmp, on the other hand, does not check for equality of its inputs, but for character-by-character equality of the arrays pointed to by its inputs (that is, it compares their members, not their addresses) and so you can use that for strings in C. It's worth noting that strcmp returns 0 (false) if the strings are equal.

Theodoros Chatzigiannakis
  • 27,825
  • 8
  • 68
  • 102