Thread overview | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 23, 2004 Appending char to char[] | ||||
---|---|---|---|---|
| ||||
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 |
January 23, 2004 Re: Appending char to char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stephan Wienczny | 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 > What about using an array? ie xyz(string ~ "!"); or char [] c = "!"; xyz(string ~ c); -- -Anderson: http://badmama.com.au/~anderson/ |
January 23, 2004 Re: Appending char to char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson | 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 >> > What about using an array? > > 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. |
January 23, 2004 Re: Appending char to char[] | ||||
---|---|---|---|---|
| ||||
"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 > >> > > What about using an array? > > > > 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: > |
January 23, 2004 Re: Appending char to char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | 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?!?
|
January 23, 2004 Re: Appending char to char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stephan Wienczny | 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/ |
January 23, 2004 Re: Appending char to char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stephan Wienczny | 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 > |
January 23, 2004 Re: Appending char to char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stephan Wienczny | "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 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?!? > 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 |
January 23, 2004 Re: Appending char to char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | 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
>
> 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?!?
>>
>
>
> 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.
|
January 23, 2004 Re: Appending char to char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to C | 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 > > > No, 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/ |
Copyright © 1999-2021 by the D Language Foundation