digitalmars.D.learn - How to handle char* to string from C functions?
- Gary Willoughby (9/9) Jan 01 2014 I'm calling an external C function which returns a string
- Adam D. Ruppe (7/10) Jan 01 2014 you can then do
- Gary Willoughby (2/13) Jan 02 2014 to!(string) works great thanks!
- Gary Willoughby (2/13) Jan 02 2014 Another question: how do i convert const(char)** to string[]?
- bearophile (8/9) Jan 02 2014 If you know that you have N strings, then a solution is
- Adam D. Ruppe (4/6) Jan 02 2014 Or if it is zero terminated, maybe
- monarch_dodra (2/8) Jan 02 2014 0-terminated arrays of non-strings :puke:
- Gary Willoughby (5/14) Jan 02 2014 Thanks, both work well:
- bearophile (11/14) Jan 02 2014 This is a feature that works with all pointers to a sequence of
- uc (2/2) Jan 02 2014 You are going to need the length of your c char*[] then a
- uc (2/2) Jan 02 2014 i'll answer in code
- John Colvin (7/12) Jan 01 2014 You're not asking to print the string, by dereferencing the
I'm calling an external C function which returns a string delivered via a char*. When i print this string out, like this: char* result = func(); writefln("String: %s", *result); I only get one character printed. I guess this is expected because i'm only returned a pointer to the first char. Instead of incrementing the pointer until i reach a null is there an easy way to convert this to a D string or get writeln to print the entire char array?
Jan 01 2014
On Wednesday, 1 January 2014 at 23:03:06 UTC, Gary Willoughby wrote:I'm calling an external C function which returns a string delivered via a char*. When i print this string out, like this: char* result = func();'you can then do string r = to!string(result); or char[] r = result[0 .. strlen(result)]; and use that/
Jan 01 2014
On Wednesday, 1 January 2014 at 23:09:05 UTC, Adam D. Ruppe wrote:On Wednesday, 1 January 2014 at 23:03:06 UTC, Gary Willoughby wrote:to!(string) works great thanks!I'm calling an external C function which returns a string delivered via a char*. When i print this string out, like this: char* result = func();'you can then do string r = to!string(result); or char[] r = result[0 .. strlen(result)]; and use that/
Jan 02 2014
On Wednesday, 1 January 2014 at 23:09:05 UTC, Adam D. Ruppe wrote:On Wednesday, 1 January 2014 at 23:03:06 UTC, Gary Willoughby wrote:Another question: how do i convert const(char)** to string[]?I'm calling an external C function which returns a string delivered via a char*. When i print this string out, like this: char* result = func();'you can then do string r = to!string(result); or char[] r = result[0 .. strlen(result)]; and use that/
Jan 02 2014
Gary Willoughby:Another question: how do i convert const(char)** to string[]?If you know that you have N strings, then a solution is (untested): pp[0 .. N].map!text.array If it doesn't work, try: pp[0 .. N].map!(to!string).array Bye, bearophile
Jan 02 2014
On Thursday, 2 January 2014 at 15:31:25 UTC, bearophile wrote:If you know that you have N strings, then a solution is (untested):Or if it is zero terminated, maybe pp.until!"a is null".map!text.array Though personally I'd just use the plain old for loop.
Jan 02 2014
On Thursday, 2 January 2014 at 15:53:40 UTC, Adam D. Ruppe wrote:On Thursday, 2 January 2014 at 15:31:25 UTC, bearophile wrote:0-terminated arrays of non-strings :puke:If you know that you have N strings, then a solution is (untested):Or if it is zero terminated, maybe pp.until!"a is null".map!text.array Though personally I'd just use the plain old for loop.
Jan 02 2014
On Thursday, 2 January 2014 at 15:31:25 UTC, bearophile wrote:Gary Willoughby:Thanks, both work well: I've noticed that const(char)** can be accessed via indexes: writefln("%s", pp[0].to!(string)); //etc. cool!Another question: how do i convert const(char)** to string[]?If you know that you have N strings, then a solution is (untested): pp[0 .. N].map!text.array If it doesn't work, try: pp[0 .. N].map!(to!string).array Bye, bearophile
Jan 02 2014
Gary Willoughby:I've noticed that const(char)** can be accessed via indexes: writefln("%s", pp[0].to!(string)); //etc. cool!This is a feature that works with all pointers to a sequence of items, like in C. But array bounds are not verified, so it's more bug-prone. So if you know the length it's better to slice the pointer as soon as possible, and then use the slice: auto ap = pp[0 .. N]; writefln("%s", ap[0].text); Or just: printf("%s\n", ap[0]); Bye, bearophile
Jan 02 2014
You are going to need the length of your c char*[] then a for-loop should do it :D
Jan 02 2014
On Wednesday, 1 January 2014 at 23:03:06 UTC, Gary Willoughby wrote:I'm calling an external C function which returns a string delivered via a char*. When i print this string out, like this: char* result = func(); writefln("String: %s", *result); I only get one character printed.You're not asking to print the string, by dereferencing the pointer you're literally asking to print the first character. If you don't want to have to walk the length of the the string to make the D array equivalent, you could always wrap it in a forward range to emulate c string handling.
Jan 01 2014