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

Calling a const char

P: n/a
I'm struggling with a bit of code that maybe some of you could help me
understand. This code processes character values from streaming data.
I'm trying to get my head around being able to get at the values of a
const char as opposed to a char variable. The code is below:

void GotLevel(const char *szLL, char MM)
{
char ss[1024];
_snprintf(ss, sizeof(ss)
, "%s %c"
, szLL, MM

The characters then get passed to a message window. The problem is when
I try to filter for certain characters. I have no problem doing that
with "char MM"... I can just use:

if(MM == 'B')

However, I can't use the same method for the szLL const char...

if(szLL == 'NYT')

I receive an error message from the compiler: '==' : 'const char *'
differs in levels of indirection from 'int'. How could I go about
filtering for the const char? I'm not understanding something here, so
if anyone sees the error in my logic, please let me know what it might
be. Any help would be very much appreciated.

Best regards,
Marcus

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Marcus wrote:
I'm struggling with a bit of code that maybe some of you could help me
understand. This code processes character values from streaming data.
I'm trying to get my head around being able to get at the values of a
const char as opposed to a char variable. The code is below:

void GotLevel(const char *szLL, char MM)
{
char ss[1024];
_snprintf(ss, sizeof(ss)
, "%s %c"
, szLL, MM

The characters then get passed to a message window. The problem is when
I try to filter for certain characters. I have no problem doing that
with "char MM"... I can just use:

if(MM == 'B')

However, I can't use the same method for the szLL const char...

if(szLL == 'NYT')

I receive an error message from the compiler: '==' : 'const char *'
differs in levels of indirection from 'int'. How could I go about
filtering for the const char? I'm not understanding something here, so
if anyone sees the error in my logic, please let me know what it might
be. Any help would be very much appreciated.


'const char*' is a pointer to what is usually a sequence of characters
ending on a character that has value 0 (so called 'null character').
Such a sequence is often called "a C string". Comparison of two C
strings is usually done using 'strcmp' function and not the equality
operator:

if (strcmp(szLL, "NYT") == 0) // the 'szLL' points to [a copy of] "NYT"
...

V
Jul 23 '05 #2

P: n/a
On Fri, 15 Apr 2005 13:48:42 -0700, Marcus wrote:
I try to filter for certain characters. I have no problem doing that
with "char MM"... I can just use:

if(MM == 'B')

However, I can't use the same method for the szLL const char...

if(szLL == 'NYT')

That is because MM is of type "char" and so is 'B'. On the other hand,
szLL is of type "const char*" and 'NYT' is a multi-character character
constant that ends up being of type char.

Are you trying to compare strings or compare characters ?

Thanks,
--
CrayzeeWulf
Jul 23 '05 #3

P: n/a
On Fri, 15 Apr 2005 21:02:48 +0000, CrayzeeWulf wrote:
That is because MM is of type "char" and so is 'B'

Well 'B' is "const char"...but you get the point.

Thanks,
--
CrayzeeWulf

Jul 23 '05 #4

P: n/a
Thanks for the explanations Victor and CrazeeWulf... I fully understand
now. Also, thanks for the 'strcmp' function code Victor it was very
enlightening. I just tested it right now and for some reason it didn't
work... turns out the "NYT" string had an additional character which is
a blank space. I thought I was doing something wrong, or didn't
understand your code correctly, but turns out "NYT" should be "NYT "
:-)

Again, thank you both for your great help.

Best regards,
Marcus

Jul 23 '05 #5

P: n/a
CrayzeeWulf wrote:
'NYT' is a multi-character character
constant that ends up being of type char.

Ooops. Another mistake. Here is what the standard says (ISO/IEC 14882:200
(E):2.13.2.1):

"An ordinary character literal that contains more than one c-char is a
multicharacter literal. A multicharacter literal has type int and
implementation-defined value."

Of course, you really meant "NYT" in the first place anyways and not 'NYT'.

Later,
--
CrayzeeWulf
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.