LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help needed using char arrays, %s, and if() in C (https://www.linuxquestions.org/questions/programming-9/help-needed-using-char-arrays-s-and-if-in-c-81685/)

KneeLess 08-13-2003 02:45 PM

Help needed using char arrays, %s, and if() in C
 
Okay. I'm trying to learn C (I already have a pretty good knoweledge of C++ and I am no newbie to programming.). Now I was just reading about %d, and %s. I decided to see if I could take a "command" if you will from the user and display output. If they typed "kneeless" then it would print "kneeless! My Name too!". But for some reason it compiles 100% fine and then always goes to the else statement, never to the if(). Here is the code:

Code:

#include <stdio.h>

int main()
{
        char word[64];
        printf("\nPlease type my name: ");
        scanf("%s", word);
        if (word == "kneeless")
        {
                printf("%s! My name too!\n", word);
        }
        else
        {
                printf("%s is not my name.\n", word);
        }
        return 0;
}

Compiles fine. Doesn't do what I want. It's probably something stupid I overlooked. Can anyone help me out?

jqcaducifer 08-13-2003 02:54 PM

in scanf you need to point the %s towards the address of word; like thus:
Code:

scanf("%s", &word);
there was some reason why scanf has to be stupid, involving reading to the memory address and stuff, which i forgot.

KneeLess 08-13-2003 02:57 PM

Right you are. Simple mistake. But doesn't solve the problem.

New code (still doesn't work, still compiles):
Code:

#include <stdio.h>

int main()
{
        char word[64];
        printf("\nPlease type my name: ");
        scanf("%s", &word);
        if (word == "kneeless")
        {
                printf("%s! My name too!\n", word);
        }
        else
        {
                printf("%s is not my name.\n", word);
        }
        return 0;
}


jqcaducifer 08-13-2003 03:08 PM

well, what does the printf in the else thing say "word" is? if it says "kneeless is not my name" then clearly there's something wrong with the if statement, but if the output is a garble of letters, then its to do with scanf and the char work[64]; part...so whats the output?

EDIT
and also try doing if (word[] == "kneeless");

EDIT2
yah...ignore everything i said and read the next post

kev82 08-13-2003 03:09 PM

do you know what a pointer is? look it up before you read on. firstly you were correct initially there should be no & in front of word because word on its own is a constant pointer to the first byte of the array. ie word == &word[0]. now for the actual problem, as i said above, word is a pointer to the first byte of the 64 byte array and "kneeless" evaluates to a constant pointer to the array {'k', 'n', 'e', 'e', 'l', 'e', 's', 's', 0} stored in the programs data section. these two pointers are NOT equal they point to completely different parts of memory. what you want to compare is not the pointers but the actual array itsself, the standard way to do this is the strcmp() function defined in string.h

modifications to your code
Code:

#include <stdio.h>
#include <string.h>

int main()
{
        char word[64];
        printf("\nPlease type my name: ");
        scanf("%s", word);
        if (!strcmp(word, "kneeless")) //strcmp returns 0 if strings equal
        {
                printf("%s! My name too!\n", word);
        }
        else
        {
                printf("%s is not my name.\n", word);
        }
        return 0;
}


coolman0stress 08-13-2003 08:42 PM

Yeah for c++ strings!

Hehe...


All times are GMT -5. The time now is 05:46 PM.