Thread overview | ||||||
---|---|---|---|---|---|---|
|
September 10, 2002 Converting char* to char[] | ||||
---|---|---|---|---|
| ||||
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 Re: Converting char* to char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russell Lewis | "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 Re: Converting char* to char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | 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 Re: Converting char* to char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | 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*). > |
Copyright © 1999-2021 by the D Language Foundation