The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
strcmp(): how does it work
Discuss strcmp(): how does it work in the C Programming forum on Dev Shed. strcmp(): how does it work C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 6th, 2003, 02:17 AM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
strcmp(): how does it work
I tried to find the code for strcmp(), but I wasn't successful. How can I look up the code in MSDN?
I imagine it compares the cstrings character by character using terms like pstring[3] and pstring2[3]. My question is aren't pstring[3] and pstring2[3] locations in memory too, so how can they ever be equal?
|

March 6th, 2003, 07:02 AM
|
|
.
|
|
Join Date: Dec 2002
Posts: 296
Time spent in forums: < 1 sec
Reputation Power: 11
|
|
|
surely it's simply what the 2 pointers *point to* that are compared. you pass it pointers to the start of each string you want to compare, and the function steps though those two strings, comparing char by char. it's not the memory addresses it compares, it's the contents of those memory addresses. that's the way i see it in any case.
|

March 6th, 2003, 07:15 AM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
Posts: 378

Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
|
|
|
Re: strcmp(): how does it work
|

March 6th, 2003, 09:54 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: Originally posted by 7stud
I imagine [strcmp] compares the cstrings character by character using terms like pstring[3] and pstring2[3]. My question is aren't pstring[3] and pstring2[3] locations in memory too, so how can they ever be equal? |
It's been a long time since I first when through it, but it seems that pointers are one of the harder things to learn in C, which is unfortunately since they are an integral part of the language and can be so very useful.
As noted and discussed elsewhere here, an array name is equivalent to a pointer -- except that you can change the value of a pointer, but not of an array name. Similarly, indexing an array is equivalent to dereferencing a pointer:
Code:
char *pChar;
char sAstring[81];
pChar = sAstring; // assign to pChar the address of the first element in sAstring
if (*pChar == sAstring[0])
printf("Dereferencing pChar is the same as indexing the first element of sAstring\n");
if (*(pChar + 3) == sAstring[3])
printf("Dereferencing (pChar+3) is the same as indexing the fourth element of sAstring\n");
That (pChar+3) is called pointer arithmetic. Please note that the compiler automatically takes into account the size of the data type that the pointer is pointing to as defined in its declaration. So (pChar++) changes the pointer by one byte, but (pInt) changes it by two under DOS/Windows.
Come to think of it, I had already been using pointers for years in Pascal before learning them in C, so my own transition was fairly smooth. But I do remember that the first time I encountered them (in PL/I), I couldn't understand what possible use there could be for them. Then the next semester I was using them all the time.
|

March 6th, 2003, 10:04 AM
|
|
Junior Member
|
|
Join Date: Mar 2003
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
On the topic of pointers.. I had troubles understanding them when I tried to learn C a few years ago. Then I took a Computer Architecture and Assembly Language course at University and it really helped my understanding of how memory is accessed. In assembly, you have registers pointing to address locations all the time, and when I moved to C after that, I found it was very easy to understand how pointers work. We even did some very low level C in the course without any libraries or anything, just the syntax, so I basically wrote my own string library for things I needed. I'm not saying you should go learn assembly language and computer architecture before you can understand pointers, but it sure made it completely clear for me!
Kevin
|

March 6th, 2003, 11:23 AM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
Posts: 378

Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
|
|
|
I agree.
Because arrays and strings are essentially pointers, it seems like it would be best to NOT have to deal with them until it is explained what pointers are used for. Learning a simple linked list or something first before ever requiring to learn strings/arrays may be nice. The biggest problems with learning languages is that when they are taught, there is not enough attention given to how it will be taught incrementally, so that at stage 1 you don't leave the student with a million questions left to be answered properly in later stages.
|

March 6th, 2003, 12:34 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
Re: strcmp(): how does it work
Quote: Originally posted by 7stud
I tried to find the code for strcmp(), but I wasn't successful. How can I look up the code in MSDN?
|
If you're looking for the actual code for strcmp(), here's one implementation:
Code:
int strcmp(s1, s2)
register const char *s1, *s2;
{
while (*s1 == *s2++)
if (*s1++ == 0)
return (0);
return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));
}
|

March 6th, 2003, 02:15 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
Jason Doucette,
I did a google search and I didn't find anything. The link you posted has the same information as in the MSDN library: return type and parameters, but not the function definition, which is what I wanted to take a look at.
balance,
I guess my thinking on what the function definition looked like was wrong. I had sort of a technical issue based on a previous thread, and from the definition Scorpions4ever posted, it looks like you were right on: instead of using terms like pstring[3], the definition uses pointer arithematic like *(pstring + 4), and so values are compared rather than locations, which I am thankful to hear otherwise it would have disrupted my understanding.
Don't let my questions besmirk the methodology for teaching C++. I learned it(and I'm relearning it) from Ivor Horton's Beginning C++, and I think the book is very good and very thorough, though maybe he could have included a tips and tricks section with the section on char pointers.
Thanks for all your responses.
|

March 6th, 2003, 02:51 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
So, is this the easiest way to assign a default string to a char* member variable:
Code:
class Test
{
private:
m_pstring;
m_value;
public:
Test()
{
m_pstring = new char[strlen("some text") + 1];
m_pstring = strcpy(m_pstring, "some text");
m_value = 0;
}
void Print()
{
cout<<m_pstring<<endl;
cout<<m_value<<endl;
}
~Test()
{
delete [] m_pstring;
}
};
Last edited by 7stud : March 6th, 2003 at 02:57 PM.
|

March 6th, 2003, 09:05 PM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
Posts: 378

Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
|
|
|

March 6th, 2003, 11:52 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
Jason,
uhhh...I could be wrong, but I think your terminology is off. In my book and in VC++ 6 that's considered a function declaration. It doesn't show you anything about the internals of the function like the definition does. For instance in your .h file you would have the function declaration:
int some_function(int a, int b);
Then in your .cpp file you would have the function definition:
int some_function(int a, int b);
{
return a + b;
}
I was trying to use language that would indicate that I was interested in looking at the funtion internals. Thanks for the link anyway.
Last edited by 7stud : March 6th, 2003 at 11:54 PM.
|

March 7th, 2003, 01:08 AM
|
|
Registered User
|
|
Join Date: Feb 2003
Location: Right Coast
Posts: 25
Time spent in forums: 21 m 34 sec
Reputation Power: 0
|
|
Its a lexicographic comparison of eash element in the array of char that the char* pointer points to (until the \0 is found).
The best thing to do would be to actually make your own strcmp to prove to yourself how it works.
If the char* is the beginning and the \0 is the end, then it is easy to see if, ASCII value by ASCII value whether two char arrays have the same contents.
J 
|

March 7th, 2003, 06:32 AM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
Posts: 378

Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
|
|
|
7stud, you are probably right. I do not know the exact terminology for such things. I was under the impression that you just wanted to know how to use the function. Most people could care less about its internals. So, when you said you wanted the definition, I assumed you didn't know how to call it. So, therefore I gave you the link. Sorry about the mix-up.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|