Dylan Little Dylan Little - 1 year ago 80
C++ Question

how to change pointer in switch statement. Error :invalid conversion from 'const char*' to 'char'

I have this code in C using emacs, in which I have already received an input, if the input uses any of the following characters I want to substitute them with the rest of the code. But I am receiving an error. What am I doing wrong?

void askQuestion(char text[])
for (char * p = text; *p; p++)
switch (*p)
case '<':
*p = "";
case '>':
*p = "";
case '#':
*p = "\n";
case '{':
*p = " \"";
case '}':
*p = "\" ";
case '[':
*p = " \'";
case ']':
*p = "\' ";

cout << *p;


and it's giving me the error

"invalid conversion from 'const char*' to 'char'

How can I modify this code to change the pointers? I just want to be able to print out the
with the substituted values. How do I do this? or what would you recommend that I do differently to make this work? or in other words how else could I write this program to modify the input.

Answer Source

This answer considers

I have this code in C

p is a char * here.

As you're taking one char at a time (switch case), you need to use a char value to populate *p, not a string, like

*p = '\n';

and so on.

Remember, a "" denotes a string literal (of type char *, in general), whereas a '' denotes a char literal (don't be surprised, it's of type int).

That said, in a C code, you can't use cout, as mentioned in the comments.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download