|
|
#1 |
|
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 |
|
|
|
0
|
|
|
#2 |
|
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'. |
|
|
|
0
|
|
|
#3 |
|
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++;
}
As stated already, strcmp() does this for you--without the bug.
|
|
|
|
0
|
|
|
#4 |
|
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. |
|
|
|
0
|
|
|
#5 | |
|
Quote:
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.");
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. |
||
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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.







Linear Mode

