D - Appending char to char[]
- Stephan Wienczny (11/11) Jan 23 2004 Hallo,
- J Anderson (9/20) Jan 23 2004 What about using an array?
- Stephan Wienczny (22/47) Jan 23 2004 and how can I convert a single char into a char[]
- Ben Hinkle (11/59) Jan 23 2004 'result' is a static array, not a dynamic array. Static array data lives...
- Stephan Wienczny (2/17) Jan 23 2004 Shouldn't the compiler generate a copy?!?
- J Anderson (4/23) Jan 23 2004 I agree, it should be simpler.
- Ben Hinkle (28/45) Jan 23 2004 on
- Stephan Wienczny (3/66) Jan 23 2004 The compiler should forbid to return static data as the result is
- J Anderson (4/5) Jan 23 2004 Exactly, copy on return. Of course the compiler could optimise that a b...
- Manfred Nowak (9/12) Jan 24 2004 How does this agree with the docs:
- J Anderson (4/17) Jan 24 2004 Well it doesn't cause an error message does it.
- C (8/19) Jan 23 2004 template TMakeArray(T) { T[] makeArray(T t ) { T[] r; r ~= t; return r; ...
- J Anderson (7/14) Jan 23 2004 No, I think it's about returning static arrays because this works...
- Ilya Minkov (3/18) Jan 23 2004 Really non-obvious, thus i flag it a bug? for now.
- Y.Tomino (12/12) Jan 23 2004 How about this ?
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); } StephanWhat about using an array? ie xyz(string ~ "!"); or char [] c = "!"; xyz(string ~ c); -- -Anderson: http://badmama.com.au/~anderson/
Jan 23 2004
J Anderson wrote:Stephan Wienczny wrote: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.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); } StephanWhat about using an array? ie xyz(string ~ "!"); or char [] c = "!"; xyz(string ~ c);
Jan 23 2004
"Stephan Wienczny" <wienczny web.de> wrote in message news:bus08a$1fg8$1 digitaldaemon.com...J Anderson 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]Stephan Wienczny wrote: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;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); } StephanWhat about using an array? ie xyz(string ~ "!"); or char [] c = "!"; xyz(string ~ 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. 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:I agree, it should be simpler. -- -Anderson: http://badmama.com.au/~anderson/'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" <wienczny web.de> wrote in message news:bus3tj$1ktm$1 digitaldaemon.com...Ben Hinkle wrote:on'result' is a static array, not a dynamic array. Static array data livesyourthe stack (I thnk). Dynamic array data lives in the heap. Why didn'tIt 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. -Benoriginal 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
Ben Hinkle wrote:"Stephan Wienczny" <wienczny web.de> wrote in message news:bus3tj$1ktm$1 digitaldaemon.com...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.Ben Hinkle wrote:on'result' is a static array, not a dynamic array. Static array data livesyourthe stack (I thnk). Dynamic array data lives in the heap. Why didn'tIt 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. -Benoriginal 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:Another way would be to only copy if used as return value.Exactly, copy on return. Of course the compiler could optimise that a bit. -- -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: [...]Well it doesn't cause an error message does it. -- -Anderson: http://badmama.com.au/~anderson/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.
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 ? CNo, I think it's about returning static arrays because this works... 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