www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to handle char* to string from C functions?

reply "Gary Willoughby" <dev nomad.so> writes:
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
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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
next sibling parent "Gary Willoughby" <dev nomad.so> writes:
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:
 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/
to!(string) works great thanks!
Jan 02 2014
prev sibling parent reply "Gary Willoughby" <dev nomad.so> writes:
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:
 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/
Another question: how do i convert const(char)** to string[]?
Jan 02 2014
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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
parent "monarch_dodra" <monarchdodra gmail.com> writes:
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:
 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.
0-terminated arrays of non-strings :puke:
Jan 02 2014
prev sibling parent reply "Gary Willoughby" <dev nomad.so> writes:
On Thursday, 2 January 2014 at 15:31:25 UTC, bearophile wrote:
 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
Thanks, both work well: I've noticed that const(char)** can be accessed via indexes: writefln("%s", pp[0].to!(string)); //etc. cool!
Jan 02 2014
parent "bearophile" <bearophileHUGS lycos.com> writes:
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
prev sibling next sibling parent "uc" <uc uc.uc> writes:
You are going to need the length of your c char*[] then a 
for-loop should do it :D
Jan 02 2014
prev sibling parent "uc" <uc uc.uc> writes:
i'll answer in code
http://dpaste.dzfl.pl/2bb1a1a8
Jan 02 2014
prev sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
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