Dylan Little Dylan Little - 25 days ago 6
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 = "";
break;
case '>':
*p = "";
break;
case '#':
*p = "\n";
break;
case '{':
*p = " \"";
break;
case '}':
*p = "\" ";
break;
case '[':
*p = " \'";
break;
case ']':
*p = "\' ";
break;
}

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
*p
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

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.