www.digitalmars.com         C & C++   DMDScript  

D - Converting char* to char[]

reply Russell Lewis <spamhole-2001-07-16 deming-os.org> writes:
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.
Sep 10 2002
parent reply "Walter" <walter digitalmars.com> writes:
"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.
Sep 10 2002
parent reply Pavel Minayev <evilone omen.ru> writes:
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*).
Sep 10 2002
parent "Walter" <walter digitalmars.com> writes:
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*).

Sep 10 2002