C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 6th, 2003, 02:17 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
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?

Reply With Quote
  #2  
Old March 6th, 2003, 07:02 AM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #3  
Old March 6th, 2003, 07:15 AM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
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?
Just do a search on google.com:

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

The first result:

http://msdn.microsoft.com/library/e...2c_._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.

Reply With Quote
  #4  
Old March 6th, 2003, 09:54 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,134 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 20 h 49 m 9 sec
Reputation Power: 1974
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.

Reply With Quote
  #5  
Old March 6th, 2003, 10:04 AM
kev123 kev123 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 5 kev123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #6  
Old March 6th, 2003, 11:23 AM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
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.

Reply With Quote
  #7  
Old March 6th, 2003, 12:34 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,387 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 39 m 3 sec
Reputation Power: 4080
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));
}

Reply With Quote
  #8  
Old March 6th, 2003, 02:15 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
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.

Reply With Quote
  #9  
Old March 6th, 2003, 02:51 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
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.

Reply With Quote
  #10  
Old March 6th, 2003, 09:05 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
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/e...2c_._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?

Reply With Quote
  #11  
Old March 6th, 2003, 11:52 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
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.

Reply With Quote
  #12  
Old March 7th, 2003, 01:08 AM
ahuimanu ahuimanu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Right Coast
Posts: 25 ahuimanu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #13  
Old March 7th, 2003, 06:32 AM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > strcmp(): how does it work

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap