By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

How to check a Const Char for truth?

P: n/a
I have this block of code, that looks something like this.
-----
if( sessionFields->__ptr[0].name == "Representative" ) { //Do
Stuff
}
-----

The first part is an object, with an array as member data. That array
is full of objects, and those objects have "name" as member data.

Those "name" variables are all of type "char*". And that variable is
filled with the value "Representatives". The problem is, this
statement never evaluates to true, so my code never executes.

I've checked to be sure that "name" contains what I expect it to
contain by sending it to "cout", and it always displays
"Representative" just like I expect.

Am I missing some level of memory indirection here? Or am I simply
doing this wrong?

Any help would be useful. Thanks!
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Thu, 20 Nov 2003 14:36:02 -0800, Ryan J. Geyer wrote:
-----
if( sessionFields->__ptr[0].name == "Representative" ) { //Do
Stuff
}
-----


Try this instead. otherwise you are comparing pointers which will never be
equal and thus your problem.

if( strcmp(sessionFields->__ptr[0].name,"Representative") == 0 )

Jul 22 '05 #2

P: n/a

"Ryan J. Geyer" <qw*****@dillholes.com> wrote in message news:bc**************************@posting.google.c om...
I have this block of code, that looks something like this.
-----
if( sessionFields->__ptr[0].name == "Representative" ) { //Do
Stuff
}
-----

The first part is an object, with an array as member data. That array
is full of objects, and those objects have "name" as member data.

Those "name" variables are all of type "char*".


Repeat after me....char* is NOT a string type.

char* is a pointer to a single char. You are testing the location of that char
against the location of the R in your string literal.

Please use std::string instead.

By the way, you are not allowed to use two consecutive underscores. That's
reserved to the compiler implementation.
Jul 22 '05 #3

P: n/a
Nathanael,

Thanks! That strcmp function did the trick! :-)

Ron,

Thanks for your input as well!

I still have issues fully comprehending data types and indirection,
which is part of the problem I had here.

As far as the double underscore, that's actually produced by a library
I am using in this app. "gSoap" for consumption of a Web Service
using the SOAP language. And, it seems happy using that, though I'm
not sure how, or why.

That's also why I didn't have the choice to change the data type to
std::string, cause that's defined by gSoap and the Web Service I am
consuming.

At any rate. Thank you BOTH for your input, as always it was
enlightening.. :-)

"Ron Natalie" <ro*@sensor.com> wrote in message news:<3f***********************@news.newshosting.c om>...
"Ryan J. Geyer" <qw*****@dillholes.com> wrote in message news:bc**************************@posting.google.c om...
I have this block of code, that looks something like this.
-----
if( sessionFields->__ptr[0].name == "Representative" ) { //Do
Stuff
}
-----

The first part is an object, with an array as member data. That array
is full of objects, and those objects have "name" as member data.

Those "name" variables are all of type "char*".


Repeat after me....char* is NOT a string type.

char* is a pointer to a single char. You are testing the location of that char
against the location of the R in your string literal.

Please use std::string instead.

By the way, you are not allowed to use two consecutive underscores. That's
reserved to the compiler implementation.

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.