Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 29th, 2006, 9:00 AM   #1
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Arrays

In C/C++, arrays (including 'C-strings', arrays of char) are not atomic objects that can be assigned or compared with the =, ==, >=, etc., operators.

The statement, "if (myString == "abcd"), will, in fact, compare the two pointers, NOT the contents referenced by those pointers.

Just thought you'd like to know, in case you don't read (or comprehend) the multidinous threads on the forum that impart this fact.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 29th, 2006, 9:30 AM   #2
zorin
Hobbyist Programmer
 
Join Date: Apr 2005
Posts: 218
Rep Power: 4 zorin is on a distinguished road
Phew...for a minuite there I thought you were asking for help
zorin is offline   Reply With Quote
Old Mar 29th, 2006, 12:18 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Hey, I ask for help when I can't find the answer by searching! (Admittedly, I search other forums as well as this one, I just don't recommend that out loud to the members here.)
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 29th, 2006, 12:48 PM   #4
MBirchmeier
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 211
Rep Power: 3 MBirchmeier is on a distinguished road
What's the context for this spontanious outburst? Has this been plagueing your thoughts recently? Or have questions like these been plagueing this (or other) forums?

-MBirchmeier
MBirchmeier is offline   Reply With Quote
Old Mar 29th, 2006, 12:52 PM   #5
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by DaWei
Hey, I ask for help when I can't find the answer by searching! (Admittedly, I search other forums as well as this one, I just don't recommend that out loud to the members here.)
I don't ask or search other forums, if I don't understand it (right away), and people here can't explain it to me, the chances are very high that at the other forums they can't explain it either (Unless my problem applies to MS specific compiler settings or the like, in which case I would go to the MS forums).
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Mar 29th, 2006, 1:49 PM   #6
Cipher
Hobbyist Programmer
 
Cipher's Avatar
 
Join Date: Feb 2005
Location: /home/cipher
Posts: 123
Rep Power: 4 Cipher is on a distinguished road
Send a message via AIM to Cipher Send a message via MSN to Cipher
So this means you can't do something like this?:

string message[1];
if (message[1] == "whatever")
etc.

That won't work?
__________________
And there was much rejoicing... Yay....
Cipher is offline   Reply With Quote
Old Mar 29th, 2006, 2:11 PM   #7
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
Quote:
Originally Posted by Cipher
So this means you can't do something like this?:

string message[1];
if (message[1] == "whatever")
etc.

That won't work?
Yes, that does work.
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old Mar 29th, 2006, 2:12 PM   #8
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by Cipher
So this means you can't do something like this?:

string message[1];
if (message[1] == "whatever")
etc.

That won't work?
Exactly. "whatever" is still a char array (C string), and thus a comparing a pointer to an object of type string, can not be done, will result in a compiler error complaining not being able to match arguments std::string, and a const char.

Edit:
Quote:
Originally Posted by jayme
Yes, that does work.
Yes it does work, but only because the string class provides the == operator.

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string message[1];
	message[0] = "hello";

	if (message[0]   == "hello")
		cout << "hello" << endl;
	else
		cout << "bye" << endl;
	return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Mar 29th, 2006, 2:15 PM   #9
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3 Seif is on a distinguished road
nope, use the string compare function.

string message[1];

if (strcmp(message[0], "whatever") ==  0)

etc

Must admit I understand where Dawai is coming from, have seen a couple of posts recently where there seems to be confusion with pointers and arrays. Was going to refer them to Dawai's tutorial on pointers as I'm not too up to scratch on them myself.
Seif is offline   Reply With Quote
Old Mar 29th, 2006, 2:17 PM   #10
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3 Seif is on a distinguished road
bah missed those last 2 posts by jayme and nnxion sorry.
Seif is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:07 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC