473,471 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2015
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. However, in declaration
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 believe the problem I'm having may be more general. ...
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 transparently made const when I pass it to the function, e.g....
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 <sstream> #include <iostream>
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 vowel="aeiou"; 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 check for initially. If I get an error for...
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 lines: isspacedLine(line); Thanks
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.