How to check a Const Char for truth?

R

Ryan J. Geyer

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!
 
N

Nathanael D. Noblet


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 )
 
R

Ron Natalie

Ryan J. Geyer said:
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.
 
R

Ryan J. Geyer

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 said:
Ryan J. Geyer said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top