472,353 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

How to check a Const Char for truth?

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
3 1941
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Sergey Tolstov | last post by:
Hello, I am working with Visual C++ 6.0 compiler. In the following declaration: int const A = 10, B = 10; both A and B are const....
3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I...
8
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 A lot of functions use const pointer arguments. If I have a non-const pointer, it is...
3
by: Al | last post by:
Hello, Sorry for the silly question: How do I insert Check Mark into RichTextBox? I can’t find the ascii code for it. Thanks Alex
4
by: Ben Pope | last post by:
Hi, The following code compiles with Comeau Online, but not MSVC 8.0. Comeau issues a warning that return 0 is unreachable. #include...
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
5
by: ivalki | last post by:
Hi How do i use the functions found in string.h to check if a letter is vowel and a consonant ? I have this code : ......... const char...
14
by: Martin Wells | last post by:
When I have errors in a program, whether they be compiler errors, linker errors, or if the program crashes when running, I have a list of things I...
15
by: puzzlecracker | last post by:
What is the quickest way to check that the following: const line; only contains whitespace, in which case to ignore it. something along these...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.