Thread overview
Converting char* to char[]
Sep 10, 2002
Russell Lewis
Sep 10, 2002
Walter
Sep 10, 2002
Pavel Minayev
Sep 10, 2002
Walter
September 10, 2002
What is the proper way to take a char* value (passed from C code) and create a dynamic array from it?  Is it possible to do this without making a copy?  You could copy it by hand, element by element, but there's got to be a better way (perhaps a cast from char* to char[] would set both the buffer and the length to the right size?).  I checked the D spec, and I can't see any reference to it.

September 10, 2002
"Russell Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3D7E3A0F.4090903@deming-os.org...
> What is the proper way to take a char* value (passed from C code) and create a dynamic array from it?  Is it possible to do this without making a copy?  You could copy it by hand, element by element, but there's got to be a better way (perhaps a cast from char* to char[] would set both the buffer and the length to the right size?).  I checked the D spec, and I can't see any reference to it.

Array slicing syntax does the job:

char *foo();
char *p = foo();
char[] b = p[0 .. strlen(p)];

I should add this to the spec.


September 10, 2002
Walter wrote:

> Array slicing syntax does the job:
> 
> char *foo();
> char *p = foo();
> char[] b = p[0 .. strlen(p)];
> 
> I should add this to the spec.

Better yet, add another function to string.d.
Could be toString(char*).

September 10, 2002
good idea

"Pavel Minayev" <evilone@omen.ru> wrote in message news:alljeo$gr7$4@digitaldaemon.com...
> Walter wrote:
>
> > Array slicing syntax does the job:
> >
> > char *foo();
> > char *p = foo();
> > char[] b = p[0 .. strlen(p)];
> >
> > I should add this to the spec.
>
> Better yet, add another function to string.d.
> Could be toString(char*).
>