3

This will compile but it is certainly not correct, I've got strange outputs. I would be glad if someone can tell me the correct way.

void test(const char c[]={'\0'});       //It will compile but strange outputs
//void test(const char c[]={'x','\0'});   //compile error

void loop() {
  const char c[] = {'u','h','u','\0'};

  test();
  test(c);
}

void test(const char c[]){
  Serial.println(c);
}
  • Sorry for the confusion. I'm working with the arduino environment and it has accepted (no warning, no error), but this here is only an abstracted code fragment, in my original code I use other names of course ;) –  Oct 09 '16 at 01:21
  • I'm guessing this is an Arduino C++ question – M.M Oct 09 '16 at 01:23

1 Answers1

3

The default initialization to zero is probably not what you want. You are actually pointing the default to NULL instead of an empty string because const char c[] turns out to be seeing by the compiler as const char *c when used as a function parameter.

You can do

void test(const char *c=""); 

instead.

I'm assuming that you are using a C++ compiler and I would recommend that you tag the question as C++ as C does not have default parameters or polymorphism.

As for the c[] syntax and behavior in a parameter you can find a good discussion here.

And as noted by CisNOTthatGOODbutISOisTHATBAD it is a bad practice to declare c as an array to avoid further confusion. Therefore although const char c[]="" would work as well, it's not a good idea.

Community
  • 1
  • 1
João Amaral
  • 321
  • 2
  • 7
  • Okay. Thanks a lot, it works. It seems to be an avr-g++ compiler for C/C++, I will remember for the next time. –  Oct 09 '16 at 01:34
  • `const char c[]=""` and `const char c[]={'\0'};` are totally equivalent, aren't they? `const char c[] = NULL;` is not, on the other hand, as the latter sets the pointer `c` to `NULL` (which is not a valid memory address of an empty string). – vsoftco Oct 09 '16 at 01:46
  • An empty string is the address of a memory that holds the \0. Which is what I suggested to change, because in this case the {'0'} is compiled as a char. In fact, before I answered the question, I tried to compile the original code in gcc 6.2 and without the -fpermissive flag it would give me an 'invalid conversion from char to char *' message. (Used https://gcc.godbolt.org/ with x86-64 gcc 6.2). I guess that when it is a parameter the rules are slightly different but I didn't check the C++ spec so I'm not sure... – João Amaral Oct 09 '16 at 01:56
  • Ohhh you're right! +1. Yes, `const char[]` decays to `const char*`, then `{'\0'}` is treated as the `char` 0, which is `NULL`. Then, when OP displays it, `std::cout` sets its error flag, so nothing else can be displayed after. – vsoftco Oct 09 '16 at 02:19
  • Down-voted for suggesting bad practices and vague explanation. Imagine @Hydra trying to acquire the size of the string pointed by the `c` parameter inside `test` by `sizeof(c)`. Which is not correct as `c` is never an array. – AnArrayOfFunctions Oct 09 '16 at 16:15
  • Your explanation doesn't say if `const char c[]` is translated to `const char *c` in a function parameter (which is true) or if it is the opposite if you understand me. You only say that they mean the same thing - but what is this thing ? - it isn't clear for me. – AnArrayOfFunctions Oct 09 '16 at 16:21
  • 1
    I actually thought it was clear enough when I said "in a function parameter is basically the same thing". I'll elaborate the answer some more though. Thanks for the feedback. – João Amaral Oct 09 '16 at 17:34