How do I get a char* that doesn't point to a zero-terminated array of characters?
How can I create "fake C-style strings" on the free store and stack?
Printable View
How do I get a char* that doesn't point to a zero-terminated array of characters?
How can I create "fake C-style strings" on the free store and stack?
Perhaps:
Code:char fake[] = {'f', 'a', 'k', 'e'};
const unsigned size = sizeof(fake);
char* fake_2 = new char[size];
std::copy(fake, fake + size, fake_2);
Why would you want to?Quote:
Originally Posted by jw232
An array is an array. It's just that when you use string literals in double quotes they are automatically terminated with '\0'.
According to what definition? There are plenty of reasons to want to manipulate strings of text which, for whatever reason, contain embedded nulls. You just can't do it with the standard string processing functions.
I don't believe the standard ever explicitly spells out what a "string" is, although it does describe what a "string literal" is, and it describes the string processing functions in terms of acting on sequences of bytes with a null terminator.
That's one good example. I guess it goes to show how deep the C-style string concept sits in most peoples' minds, that you have a hard time imagining a string with a null in it.
Anyway, this is a valid string which contains a null: "This string \0 has a null in it." It's not for us to ask why :)
File dialog filters usually have a number of embedded \0 to separate the parts.
Ah, I was looking at C-style strings, not char buffers.