Calling a const char

M

Marcus

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
 
V

Victor Bazarov

Marcus said:
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
 
C

CrayzeeWulf

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,
 
M

Marcus

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
 
C

CrayzeeWulf

CrayzeeWulf said:
'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,
 

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
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top