How to initialize "unsigned char *" with default argument in C++?

RSFalcon7 picture RSFalcon7 · Nov 28, 2013 · Viewed 8.7k times · Source

I have a class with a method with the following signature:

void print(unsigned char *word);

I need to set "" as default value for word, how can I do that?

I tried the obvious void print(unsigned char *word=""); but I got the following error:

error: cannot initialize a parameter of type
  'unsigned char *' with an lvalue of type 'const char [1]'
    void print(unsigned char *word="");

Since I can't initialize word with a string literal who should I do it?

Answer

Lightness Races in Orbit picture Lightness Races in Orbit · Nov 28, 2013

You say that this is a "prefix" argument to apply to the printing.

The answer is that you should make the argument const, stop doing whatever mutations you're doing to it inside the function, and then use "" as a default argument:

void print(const char* prefix = "")