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?
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);
Last edited by anon; 04-22-2008 at 01:54 PM.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Why would you want to?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'.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
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.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler