March 6th, 2003, 03:17 AM
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 and pstring2. My question is aren't pstring and pstring2 locations in memory too, so how can they ever be equal?
March 6th, 2003, 08:02 AM
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, 08:15 AM
March 6th, 2003, 10:54 AM
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:
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.
pChar = sAstring; // assign to pChar the address of the first element in sAstring
if (*pChar == sAstring)
printf("Dereferencing pChar is the same as indexing the first element of sAstring\n");
if (*(pChar + 3) == sAstring)
printf("Dereferencing (pChar+3) is the same as indexing the fourth element of sAstring\n");
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, 11:04 AM
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!
March 6th, 2003, 12:23 PM
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, 01:34 PM
Re: strcmp(): how does it work
If you're looking for the actual code for strcmp(), here's one implementation:
int strcmp(s1, s2)
register const char *s1, *s2;
while (*s1 == *s2++)
if (*s1++ == 0)
return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));
March 6th, 2003, 03:15 PM
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.
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, 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, 03:51 PM
So, is this the easiest way to assign a default string to a char* member variable:
m_pstring = new char[strlen("some text") + 1];
m_pstring = strcpy(m_pstring, "some text");
m_value = 0;
delete  m_pstring;
Last edited by 7stud; March 6th, 2003 at 03:57 PM.
March 6th, 2003, 10:05 PM
March 7th, 2003, 12:52 AM
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 7th, 2003 at 12:54 AM.
March 7th, 2003, 02:08 AM
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.
March 7th, 2003, 07:32 AM
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.