Thread overview
array concatenation
Jul 11, 2005
Mike
Jul 11, 2005
Ben Hinkle
Jul 11, 2005
terry
July 11, 2005
Hi

What is the correct way to initialize a dynamic array with a single element? This doesnt seem satisfactory to me:

char[] array;
char c;
...
array = null;
array ~= c;

Is there a better way of doing this without using casts?


July 11, 2005
"Mike" <Mike_member@pathlink.com> wrote in message news:datv7a$1dfh$1@digitaldaemon.com...
> Hi
>
> What is the correct way to initialize a dynamic array with a single
> element?
> This doesnt seem satisfactory to me:
>
> char[] array;
> char c;
> ...
> array = null;
> array ~= c;
>
> Is there a better way of doing this without using casts?
>
>

Why do you need the 'array=null' line? It already starts out empty.
There have been requests for an api to construct an array using an
expression like array!(char)(c) or something but that will most likely get
attention after 1.0. For now one can roll such a feature by hand - I recall
a post after Walter added typesafe varargs implementing such a template so
searching either the announce or main newsgroup will probably turn up
something.

ps - the digitalmars.d.learn newsgroup might be better suited for this thread.


July 11, 2005
In article <datv7a$1dfh$1@digitaldaemon.com>, Mike says...
>
>Hi
>
>What is the correct way to initialize a dynamic array with a single element? This doesnt seem satisfactory to me:
>
>char[] array;
>char c;
>...
>array = null;
>array ~= c;
>
>Is there a better way of doing this without using casts?
>
>

Do you mean like this?

char [] array = "c";
array.length = 5;
array[1..5] = "defg";
printf("%.*s\n",array);