String returns (null) instead of blank

Hi,

String (str.c_str()) returns (null) instead of blank, this poses a problem when I use http request because the result is GET / (null) HTTP / 1.1 instead of GET / HTTP / 1.1. even if I display it on a lcd, I have the same thing as a result: GET / (null) HTTP / 1.1

Also, if I use sprintf(buf, "%s, ""), result is (null) instead of a blank character

it's possible to replace (null) by a blank character?

thanks for your help.

What board are you using? I don't see this behavior on a Uno.

what do you mean by String (str.c_str())why would you create a new String when you already have str ??

why do you think [color=blue]""[/color] should be a blank? it denotes an empty string, one with 1 byte which is the NULL character.

try this code:

const char* s = "";
char buffer[10];

String fooString = "";

void setup()
{
  Serial.begin(115200);
  Serial.println();
  
  Serial.println(strlen(s));  // will show '0'

  sprintf(buffer, "%s", s);

  Serial.print(F("Buffer is: ["));
  Serial.print(buffer);
  Serial.println(F("]")); // will show 'Buffer is: []'


  Serial.print(F("fooString is: ["));
  Serial.print(fooString);
  Serial.println(F("]")); // will show 'fooString is: []'

}

void loop() {}

You'll see the length of the cString is 0 - which means 1 byte was allocated the trailing NULL that is part of the standard.

console will show

[color=purple]
0
Buffer is: []
fooString is: []
[/color]

which is expected.

(tested on a UNO)