LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-21-2004, 09:51 AM   #1
WindowsBurner
Member
 
Registered: Nov 2003
Location: In chaos
Distribution: OpenSuse
Posts: 293

Rep: Reputation: 30
Error comparing strings and acting on the comparison


Hi,

How come when I do :
Code:
int main()                                                                                                                                     
{                                                                                                                                              
   char input[6];                                                                                                                              
   scanf("%s", input);                                                                                                                         
   if(input == "look")                                                                                                                         
     printf("You typed : look\n");                                                                                                             
   else if(input =="quit")                                                                                                                    
     exit(0);                                                                                                                                  
   else                                                                                                                                        
     printf("You did not type a choice.\n");                                                                                                   
                                                                                                                                               
   return 0;                                                                                                                                   
}
I always get the message : You did not type a choice.?
I tried typing both :
Code:
look
and
Code:
quit
plus a few other things, but to no avail.
I tried replacing the double quotes with single quotes and I tried replacing the double equals with one equals and it still didn't work, though I got errors( I knew I would, but I'm trying everything).

With single-quotes :
Code:
test.c:7:16: warning: multi-character character constant
test.c: In function `main':
test.c:7: warning: comparison between pointer and integer
test.c:9:21: warning: multi-character character constant
test.c:9: warning: comparison between pointer and integer
With one equals :
Code:
test.c:7:15: warning: multi-character character constant
test.c: In function `main':
test.c:7: incompatible types in assignment
test.c:9:20: warning: multi-character character constant
test.c:9: incompatible types in assignment
I'm completely friggin baffled, I've had this problem since I started programming and I've never been able to figure out why.

Any help would be greatly appreciated. Thanks!

- WB
 
Old 10-21-2004, 10:13 AM   #2
mfeat
Member
 
Registered: Aug 2003
Location: Akron, OH
Distribution: Fedora Core 3
Posts: 185

Rep: Reputation: 30
change:
if(input == "look")
to:
if(strcmp(input, "look") == 0)
 
Old 10-21-2004, 10:42 AM   #3
WindowsBurner
Member
 
Registered: Nov 2003
Location: In chaos
Distribution: OpenSuse
Posts: 293

Original Poster
Rep: Reputation: 30
Thanks a lot mfeat. Thats alot better than the workaround I came up with. I was using a while loop and getchar to get around it.
But now that I have two work arounds.... Can you or someone else tell me WHY my previous post does not work?

I know it doesn't but my big problem is WHY. I know most of you wouldn't care, but I've got a crazy curiosity and its gonna bug me till I find out. Dont think I'm just trying to get an easy answer to my problem, I've spent easily 100 hours trying to work on that problem and read easily 100 tutorials and 4 or 5 books. Plus at least 50 hours of testing different ways myself.

As you can probably tell I'm not the best programmer since I spent all that time without fixing my problem.

Thanks again mfeat for helping. And thanks to anyone who can answer my WHY.

- WB
 
Old 10-21-2004, 11:48 AM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
String literals in C (e.g. "look") are stored somewhere in memory. When you use "look" in your code you're actually playing with a pointer to that stored memory. I don't know how familiar you are with pointers, but basically what you're doing is comparing 2 memory addresses when you do if(input == "look").

So it's not comparing the contents of the strings, but the memory address of the strings. So:
Code:
char *ptr1 = "foo";
char *ptr2 = "foo";

printf("%d\n", ptr1 == ptr2)
that will print 0 since there's actually 2 "foo"s in memory and each pointer points to its own, but:
Code:
char *ptr1 = "foo";
char *ptr2 = ptr1;

printf("%d\n", ptr1 == ptr2);
that will print 1 because the value of ptr1 is equal to the value of ptr2 (they point to the same address).

You can also look at it like this:
Code:
int a = 5;
int b = 5;

printf("%d\n", a == b);
printf("%d\n", &a == &b);
The first printf() will print 1 because the values of a and b are the same, but the second printf() will print 0 because the address of a and b are not the same.

So to sum up, strings in C are really just an address to the first character in the string. When you do if(string1 == string2) you're comparing the memory addresses that the two strings point to, not the contents of the string.
 
Old 10-21-2004, 12:37 PM   #5
WindowsBurner
Member
 
Registered: Nov 2003
Location: In chaos
Distribution: OpenSuse
Posts: 293

Original Poster
Rep: Reputation: 30
Thanks a friggin million itsme86, NOW I finally understand. *Whew* now my curiosity can relax.
Thanks again.

- WB
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
Comparing 2 Files xianzai Programming 2 05-23-2004 11:50 AM
Comparing filesystems for changes Astryk Linux - Software 1 04-12-2004 10:56 AM
Lilo error and KPP acting strange - please help! bmuseful Mandriva 6 10-02-2003 10:14 AM
C programming error. warning: comparison between pointer and integer Linh Programming 4 06-06-2003 03:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration