#1
  1. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15

    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?
  2. #2
  3. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    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.
  4. #3
  5. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12

    Re: strcmp(): how does it work


    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?
    Just do a search on google.com:

    http://www.google.com/search?q=msdn+strcmp

    The first result:

    http://msdn.microsoft.com/library/en...c_._mbscmp.asp

    Just as dwise1_aol mentioned in the other thread ( http://forums.devshed.com/t54168/s.html ), the result of this function is not what is normally expected. I just got burned by it last week, so take care in using it.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    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.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    5
    Rep 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
  10. #6
  11. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    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.
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,615
    Rep Power
    4247

    Re: strcmp(): how does it work


    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));
    }
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    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.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    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.
  18. #10
  19. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    7stud, Doing the search that I stated ( http://www.google.com/search?q=msdn+strcmp ), and looking at the first result ( http://msdn.microsoft.com/library/en...c_._mbscmp.asp ) shows this at the very top of the page:

    "Run-Time Library Reference

    strcmp, wcscmp, _mbscmp

    Compare strings.

    int strcmp(
    const char *
    string1,
    const char *
    string2
    );
    "

    Isn't this the function definition you were looking for?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    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.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Right Coast
    Posts
    25
    Rep 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 :)
  24. #13
  25. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    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.

IMN logo majestic logo threadwatch logo seochat tools logo