D - Appending char to char[]
- Stephan Wienczny <wienczny web.de> Jan 23 2004
- J Anderson <REMOVEanderson badmama.com.au> Jan 23 2004
- Stephan Wienczny <wienczny web.de> Jan 23 2004
- "Ben Hinkle" <bhinkle4 juno.com> Jan 23 2004
- Stephan Wienczny <wienczny web.de> Jan 23 2004
- J Anderson <REMOVEanderson badmama.com.au> Jan 23 2004
- "Ben Hinkle" <bhinkle4 juno.com> Jan 23 2004
- Stephan Wienczny <wienczny web.de> Jan 23 2004
- J Anderson <REMOVEanderson badmama.com.au> Jan 23 2004
- Manfred Nowak <svv1999 hotmail.com> Jan 24 2004
- J Anderson <REMOVEanderson badmama.com.au> Jan 24 2004
- "C" <dont respond.com> Jan 23 2004
- J Anderson <REMOVEanderson badmama.com.au> Jan 23 2004
- Ilya Minkov <minkov cs.tum.edu> Jan 23 2004
- "Y.Tomino" <demoonlit inter7.jp> Jan 23 2004
Hallo,
what is the standard D way of appending a char to a char[]?
I've go a function expecting a char[]. How do I convert a char to a
char[] in a function call?
void xyz(char[] abc);
{
char[] string = "My string";
char c = '!';
xyz(string ~ c);
}
Stephan
Jan 23 2004
Stephan Wienczny wrote:Hallo, what is the standard D way of appending a char to a char[]? I've go a function expecting a char[]. How do I convert a char to a char[] in a function call? void xyz(char[] abc); { char[] string = "My string"; char c = '!'; xyz(string ~ c); } Stephan
ie xyz(string ~ "!"); or char [] c = "!"; xyz(string ~ c); -- -Anderson: http://badmama.com.au/~anderson/
Jan 23 2004
J Anderson wrote:Stephan Wienczny wrote:Hallo, what is the standard D way of appending a char to a char[]? I've go a function expecting a char[]. How do I convert a char to a char[] in a function call? void xyz(char[] abc); { char[] string = "My string"; char c = '!'; xyz(string ~ c); } Stephan
ie xyz(string ~ "!"); or char [] c = "!"; xyz(string ~ c);
and how can I convert a single char into a char[] I've written a function trying to do that, but I get unexpected results import std.stream; char[] Char2String(char c) { char[1] result = c; debug { stdout.write("Char2String result: "~result~" input: "); stdout.write(c); stdout.writeLine(""); } return result; } int main(char[][] argv) { stdout.writeLine(Char2String('!')); stdout.writeLine("end."); return 0; } The debug output is correct but the result is not.
Jan 23 2004
"Stephan Wienczny" <wienczny web.de> wrote in message news:bus08a$1fg8$1 digitaldaemon.com...J Anderson wrote:Stephan Wienczny wrote:Hallo, what is the standard D way of appending a char to a char[]? I've go a function expecting a char[]. How do I convert a char to a char[] in a function call? void xyz(char[] abc); { char[] string = "My string"; char c = '!'; xyz(string ~ c); } Stephan
ie xyz(string ~ "!"); or char [] c = "!"; xyz(string ~ c);
and how can I convert a single char into a char[] I've written a function trying to do that, but I get unexpected results import std.stream; char[] Char2String(char c) { char[1] result = c;
'result' is a static array, not a dynamic array. Static array data lives on the stack (I thnk). Dynamic array data lives in the heap. Why didn't your original code work? If you really want a function to do it try something like char[] Char2String(char c) { char[] result; result ~= c; [snip]debug { stdout.write("Char2String result: "~result~" input: "); stdout.write(c); stdout.writeLine(""); } return result; } int main(char[][] argv) { stdout.writeLine(Char2String('!')); stdout.writeLine("end."); return 0; } The debug output is correct but the result is not. I would expect that it outputs:
Jan 23 2004
Ben Hinkle wrote:'result' is a static array, not a dynamic array. Static array data lives on the stack (I thnk). Dynamic array data lives in the heap. Why didn't your original code work? If you really want a function to do it try something like char[] Char2String(char c) { char[] result; result ~= c; [snip]
Shouldn't the compiler generate a copy?!?
Jan 23 2004
Stephan Wienczny wrote:Ben Hinkle wrote:'result' is a static array, not a dynamic array. Static array data lives on the stack (I thnk). Dynamic array data lives in the heap. Why didn't your original code work? If you really want a function to do it try something like char[] Char2String(char c) { char[] result; result ~= c; [snip]
Shouldn't the compiler generate a copy?!?
I agree, it should be simpler. -- -Anderson: http://badmama.com.au/~anderson/
Jan 23 2004
"Stephan Wienczny" <wienczny web.de> wrote in message news:bus3tj$1ktm$1 digitaldaemon.com...Ben Hinkle wrote:'result' is a static array, not a dynamic array. Static array data lives
the stack (I thnk). Dynamic array data lives in the heap. Why didn't
original code work? If you really want a function to do it try something like char[] Char2String(char c) { char[] result; result ~= c; [snip]
Shouldn't the compiler generate a copy?!?
It is like in C when you declare a "static array" and return a pointer to it. I could see arguments for either behavior - C compatibility or implicit copying. Consider also how assignment/casting works, though. If you write char [] a; char [10] b; a = b; then both a and b point to the same data. If returning a static array made a copy of the data then one could argue that the assignment a=b should also make a copy. Otherwise char[] foo() { char[10] b; return b; } would create a copy but char[] foo() { char[10] b; char[] a; a = b; return a; } wouldn't. -Ben
Jan 23 2004
Ben Hinkle wrote:"Stephan Wienczny" <wienczny web.de> wrote in message news:bus3tj$1ktm$1 digitaldaemon.com...Ben Hinkle wrote:'result' is a static array, not a dynamic array. Static array data lives
onthe stack (I thnk). Dynamic array data lives in the heap. Why didn't
youroriginal code work? If you really want a function to do it try something like char[] Char2String(char c) { char[] result; result ~= c; [snip]
Shouldn't the compiler generate a copy?!?
It is like in C when you declare a "static array" and return a pointer to it. I could see arguments for either behavior - C compatibility or implicit copying. Consider also how assignment/casting works, though. If you write char [] a; char [10] b; a = b; then both a and b point to the same data. If returning a static array made a copy of the data then one could argue that the assignment a=b should also make a copy. Otherwise char[] foo() { char[10] b; return b; } would create a copy but char[] foo() { char[10] b; char[] a; a = b; return a; } wouldn't. -Ben
The compiler should forbid to return static data as the result is undefined. Another way would be to only copy if used as return value.
Jan 23 2004
Stephan Wienczny wrote:Another way would be to only copy if used as return value.
-- -Anderson: http://badmama.com.au/~anderson/
Jan 23 2004
Stephan Wienczny schrieb: [...]The compiler should forbid to return static data as the result is undefined. Another way would be to only copy if used as return value.
How does this agree with the docs: | It is an error to return the address of or a reference to a local | variable. So long. -- Fight Spam! Join EuroCAUCE: http://www.euro.cauce.org/ 2EA56D6D4DC41ABA311615946D3248A1
Jan 24 2004
Manfred Nowak wrote:Stephan Wienczny schrieb: [...]The compiler should forbid to return static data as the result is undefined. Another way would be to only copy if used as return value.
How does this agree with the docs: | It is an error to return the address of or a reference to a local | variable. So long.
Well it doesn't cause an error message does it. -- -Anderson: http://badmama.com.au/~anderson/
Jan 24 2004
template TMakeArray(T) { T[] makeArray(T t ) { T[] r; r ~= t; return r; } }
char [] foo = TMakeArray!(char).makeArray('T') ~ "his is only a test";
Is usually what I do , I don't know how effecient it is :/. I'm also not
sure why making r "T[1] r;r[0] = t;" doesnt work , you can only append
dynamic arrays ?
C
"Stephan Wienczny" <wienczny web.de> wrote in message
news:burtne$1ahk$1 digitaldaemon.com...
Hallo,
what is the standard D way of appending a char to a char[]?
I've go a function expecting a char[]. How do I convert a char to a
char[] in a function call?
void xyz(char[] abc);
{
char[] string = "My string";
char c = '!';
xyz(string ~ c);
}
Stephan
Jan 23 2004
C wrote:template TMakeArray(T) { T[] makeArray(T t ) { T[] r; r ~= t; return r; } } char [] foo = TMakeArray!(char).makeArray('T') ~ "his is only a test"; Is usually what I do , I don't know how effecient it is :/. I'm also not sure why making r "T[1] r;r[0] = t;" doesnt work , you can only append dynamic arrays ? C
char[1] result; result[0] = '!'; printf("%.*s", (string ~ result)); -- -Anderson: http://badmama.com.au/~anderson/
Jan 23 2004
Really non-obvious, thus i flag it a bug? for now. -eye Stephan Wienczny wrote:Hallo, what is the standard D way of appending a char to a char[]? I've go a function expecting a char[]. How do I convert a char to a char[] in a function call? void xyz(char[] abc); { char[] string = "My string"; char c = '!'; xyz(string ~ c); } Stephan
Jan 23 2004
How about this ?
void xyz(char[] abc)
{
printf("%.*s", abc);
}
void main()
{
char[] string = "My string";
char c = '!';
xyz(string ~ (&c)[0..1]);
}
YT
Jan 23 2004









Stephan Wienczny <wienczny web.de> 