www.digitalmars.com         C & C++   DMDScript  

D - Appending char to char[]

reply Stephan Wienczny <wienczny web.de> writes:
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
next sibling parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
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/
Jan 23 2004
next sibling parent Stephan Wienczny <wienczny web.de> writes:
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.
Jan 23 2004
prev sibling parent reply "Ben Hinkle" <bhinkle4 juno.com> writes:
"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:
Jan 23 2004
parent reply Stephan Wienczny <wienczny web.de> writes:
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
next sibling parent J Anderson <REMOVEanderson badmama.com.au> writes:
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
prev sibling parent reply "Ben Hinkle" <bhinkle4 juno.com> writes:
"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
Jan 23 2004
parent reply Stephan Wienczny <wienczny web.de> writes:
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.
Jan 23 2004
next sibling parent J Anderson <REMOVEanderson badmama.com.au> writes:
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
prev sibling parent reply Manfred Nowak <svv1999 hotmail.com> writes:
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
parent J Anderson <REMOVEanderson badmama.com.au> writes:
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
prev sibling next sibling parent reply "C" <dont respond.com> writes:
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
parent J Anderson <REMOVEanderson badmama.com.au> writes:
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/
Jan 23 2004
prev sibling next sibling parent Ilya Minkov <minkov cs.tum.edu> writes:
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
prev sibling parent "Y.Tomino" <demoonlit inter7.jp> writes:
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