Fpermissive going to kill me

Can someone please tell me which variables are char and const char* in this message?

In function 'void logger(int, char, float)':
main:1001:21: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
     strcpy(tmp, text);
                     ^

This is the code, stripped for testing purposes:

void logger(int level,char text,float val /*=NULL*/) {
 
  char tmp[450];
  strcpy(tmp, text);

}

Apparently I will die without understanding how chars work but I am trying. Part of what I don't understand here is where the const designation is coming from. The supposed-to-be-helpful carat is pointing to a close parenthesis. So which variable is magically a const when I've declared neither as such? (At least not on purpose).

I believe you meant to write

void logger(int level,char *text,float val /*=NULL*/) {

Notice the "*" in front of text? That makes it a pointer to char rather than a char.

strcpy(char *,char); is what it ended up objecting to.

Text is just a char. strcpy expects an array of char.

Passing an array is done by pointers. Thus strcpy is expecting a pointer to an array of char.

tmp[450] is a (very large) array of char. tmp (without any brackets) is a pointer to that array.

Thank you both for your replies, which do make things clearer for me. But what about the "const" in the error message. Are char arrays constant by default? It feels misleading to me when I haven't declared tmp as const char yet the error indicates a const char is involved here.

Just look up the definition of strcpy:

char * strcpy ( char * destination, const char * source );
1 Like

No, but strcpy is basically promising not to change whatever is in the second element. You are passing your array pointer to a char array and it is promising to treat it as a const char array and not change it in any way.

tmp is not the one it is complaining about. It's text that is a single char and not a pointer to a char array.

1 Like

And once again the brick wins because after banging my head on it long enough, I couldn't see the simple answer.

Thanks again.

This is a really great explanation. Wish I could mark two solutions.

You may only be able to give one :white_check_mark: but you can give as many :heart: as you like. :slight_smile:

It's ok. I got lots and lots of checkmarks. I don't keep track.