Reputation: 41
I want to return a null-terminated char* that contains a backspace character without malloc
'ing it.
Specifically, I want the string to be {backspace-character, space-character, backspace-character, null-character} and nothing else.
For a "regular" string, I know I could say
char* s = "regular";
return s;
Then s
is safe to pass around to other functions because it points to the string regular
, which is a global declared outside of the stack. However, the only way I can think of accomplishing this with backspace would be
char* s = {0x08, ' ', 0x08, '\0'};
return s;
but this seems problematic because now the array I declared is on the stack, and will not be valid for use in the calling function. Again, I don't want to malloc
this string because I don't want to deal with having to free
it later. What can I do?
Upvotes: 0
Views: 714
Reputation: 16406
In addition to using a string constant per the other answers, you can do
static char s[] = {0x08, ' ', '0x08, '\0'};
return s;
Upvotes: 0
Reputation: 11963
You can include a backspace in a string literal as \b
e.g. "\b \b"
Upvotes: 3