Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

From Rules for C++ string literals escape characterRules for C++ string literals escape character ,Eli's answer

std::string ("0\0" "0", 3) // String concatenation 

works because this version of the constructor takes a char array; if you try to just pass "0\0" "0" as a const char*, it will treat it as a C string and only copy everything up until the null character.

Does that mean space isn't alloted for entire string , ie the string after \0 is written on unalloted space ?

Moreover the above question is for c++ string, I observed same behaviour for c strings too . Are c and c++ strings same when I add null char in middle of string during declaration ?

From Rules for C++ string literals escape character ,Eli's answer

std::string ("0\0" "0", 3) // String concatenation 

works because this version of the constructor takes a char array; if you try to just pass "0\0" "0" as a const char*, it will treat it as a C string and only copy everything up until the null character.

Does that mean space isn't alloted for entire string , ie the string after \0 is written on unalloted space ?

Moreover the above question is for c++ string, I observed same behaviour for c strings too . Are c and c++ strings same when I add null char in middle of string during declaration ?

From Rules for C++ string literals escape character ,Eli's answer

std::string ("0\0" "0", 3) // String concatenation 

works because this version of the constructor takes a char array; if you try to just pass "0\0" "0" as a const char*, it will treat it as a C string and only copy everything up until the null character.

Does that mean space isn't alloted for entire string , ie the string after \0 is written on unalloted space ?

Moreover the above question is for c++ string, I observed same behaviour for c strings too . Are c and c++ strings same when I add null char in middle of string during declaration ?

this is purely C++
Link
Jens Gustedt
  • 79.3k
  • 9
  • 110
  • 184
Source Link
Zxcv Mnb
  • 743
  • 1
  • 8
  • 20

null character inside string

From Rules for C++ string literals escape character ,Eli's answer

std::string ("0\0" "0", 3) // String concatenation 

works because this version of the constructor takes a char array; if you try to just pass "0\0" "0" as a const char*, it will treat it as a C string and only copy everything up until the null character.

Does that mean space isn't alloted for entire string , ie the string after \0 is written on unalloted space ?

Moreover the above question is for c++ string, I observed same behaviour for c strings too . Are c and c++ strings same when I add null char in middle of string during declaration ?