Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 17, 2010, 01:03 PM   #1
SAEinSDSU
macrumors newbie
 
Join Date: Feb 2010
String recognition problems? (Basic)

Okay this is my first post on these forums with a very basic problem i have run into. I cannot seem to understand why my strings will not recognize each other as being the same. if i have code

char a[] = "test";
char b[] = "test";
if(a == b){
printf("%S", a);
}
else
return (0);

it will not print out a. Is this because the compiler cannot recognize these strings as equal by address or am i just writing the code wrong? Any answers would be great! thanks
SAEinSDSU is offline   0 Reply With Quote
Old Feb 17, 2010, 01:18 PM   #2
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: Des Moines, WA
Although you haven't specified the language I will assume by your stated results that it is 'C'. If true then the use of '==' is comparing the addrresses of each string 'a' and 'b' and determining that they are not the same since their addresses are different.

You need to look into the standard 'C' library function 'strcmp'.
lloyddean is offline   0 Reply With Quote
Old Feb 17, 2010, 02:29 PM   #3
Detrius
macrumors 68000
 
Join Date: Sep 2008
Location: Asheville, NC
or another way of saying it:

You have a blue ball in your left hand and a blue ball in your right hand. Your comparison checks to see if the two balls are in the same hand--not whether the balls themselves are the same.

A string is just an array of characters, and in C, if you reference an array without an index, you're just referencing the pointer value--the address of the first element in the array. Instead, you need to step through the arrays and compare each element, roughly like this:

Code:
bool are_equal = true;
int x=0;
while( ( a[x] != '\0' ) && ( b[x] != '\0' ) )
{
    if ( a[x] != b[x] )
        are_equal = false;
    x++;
}
Technically, this code doesn't take into account that one string could be longer than the other, but you get the idea.

As stated already, strcmp() does this for you--without the bug.
Detrius is offline   0 Reply With Quote
Old Feb 17, 2010, 09:41 PM   #4
SAEinSDSU
Thread Starter
macrumors newbie
 
Join Date: Feb 2010
Okay so yes I understand the address confusion situation and follow your explanation of another way of doing it det...thank you!

Would it be possible to compare the two strings by address in this case as an alternative method? Or is that what the string compare standard function does.
SAEinSDSU is offline   0 Reply With Quote
Old Feb 17, 2010, 10:04 PM   #5
Detrius
macrumors 68000
 
Join Date: Sep 2008
Location: Asheville, NC
Quote:
Originally Posted by SAEinSDSU View Post
Would it be possible to compare the two strings by address in this case as an alternative method? Or is that what the string compare standard function does.
There's nothing to stop you from doing an address compare, but there's no reason you would want to here. That's what my metaphor about the two balls in your two hands is supposed to convey. You have two different arrays. An address compare will always come up not-equal, no matter what's in the arrays. It will only tell you if you have two pointers that reference the same chunk of memory. That is, it will tell you if you have two hands on the same ball--not whether you are holding two identical balls.

The string compare function is better than my simple example in that it doesn't have the string-length bug, but this is roughly what it would do--walk through the strings comparing each character one at a time.


This is where an address compare would work:

Code:
const char string_one[] = "a string";
const char string_two[] = "a string";

char * a_pointer = string_one;

if ( a_pointer == string_two )
    printf("this is not possible");

if ( a_pointer == string_one )
    printf("we stored the address of the first string.");
About the only way an address compare is going to be useful is in a highly contrived example. Chances are that it's not what you want. It's possible that it could be useful at some point, but there's probably a clearer way to write your code if you're ever thinking it's a good idea.

One exception: you should always do an address compare inside operator=() in C++, as there's no reason to bother with the assignment code if you're assigning an object to itself. But really, that's just a safeguard.
Detrius is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
iMac CD recognition problem - help wexford iMac 0 May 18, 2008 11:36 AM
external drive recognition problem gerardg Mac Basics and Help 3 May 11, 2008 07:43 AM
SMS sender recognition problem after 1.1.4 upgrade cleung iPhone Tips, Help and Troubleshooting 1 Apr 27, 2008 10:20 PM
external HD recognition problem. ~Lukasdp Mac Basics and Help 3 Nov 17, 2007 12:34 AM
Internal Hard Drive Recognition Problem mms Mac OS X 1 Oct 27, 2005 04:00 PM


All times are GMT -5. The time now is 10:55 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC