472,373 Members | 1,552 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Calling a const char

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

Similar topics

2
by: William Payne | last post by:
Hello, consider these following two classes. A base class, class MDIChildWindow, and a class inherting from that base class, class Document. In the static base member function callback() I obtain a...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
11
by: j23 | last post by:
I have library (static) testlib.cpp: #include <stdarg.h> void xxx(...) { char buf; va_list args; va_start(args, buf); va_end(args); }
1
by: Nick Bishop | last post by:
I have a problem where I call a method in a C++ class with a pointer which is a static member in that class. When I use a debugger, I see the pointer having a certain value, but when I step into...
8
by: Plissken.s | last post by:
I have a template function which print out the content of a list: The following compiles: void PrintInt2 (int i) { cout << i << " "; } template <class T> void printList(T& list) {
6
by: RB Smissaert | last post by:
Made a C++ dll with MS VC6 and trying to call the dll from Excel VBA. This is the code in the .cpp file: #include "stdafx.h" #include <string> #include <math.h> using namespace std;
5
by: Joakim Hove | last post by:
Hello, I have a function like this: /* It actually does something else .... */ void func (int size, const char ** string_list) { int i; for (i=0; i < size; i++) printf("String number %d:...
2
by: kalyani1031 | last post by:
hi, Iam calling CPP function in C , I get the following undefined references. ./mycppinc/libmycppinc.a(cpp_file.o)(.text+0xd): In function `std::__verify_grouping(char const*, unsigned int,...
61
by: Ron Ford | last post by:
K&R has three different versions of qsort, and the ultimate one is supposed to be like the one in the std library. I'm trying to implement the first, which is in 4.10. I think I'm pretty close...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
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 server and have made sure to enable curl. I get a...
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. Background colors can be used to highlight important...
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 has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.