ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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;
}
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
Last edited by jqcaducifer; 08-13-2003 at 03:10 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;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.