Jump to page: 1 2
Thread overview
How to handle char* to string from C functions?
Jan 01, 2014
Gary Willoughby
Jan 01, 2014
Adam D. Ruppe
Jan 02, 2014
Gary Willoughby
Jan 02, 2014
Gary Willoughby
Jan 02, 2014
bearophile
Jan 02, 2014
Adam D. Ruppe
Jan 02, 2014
monarch_dodra
Jan 02, 2014
Gary Willoughby
Jan 02, 2014
bearophile
Jan 02, 2014
uc
Jan 02, 2014
uc
Jan 01, 2014
John Colvin
January 01, 2014
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?
January 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/
January 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();
>
> 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.
January 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:
>> 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!
January 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:
>> 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[]?
January 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
January 02, 2014
You are going to need the length of your c char*[] then a for-loop should do it :D
January 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.
January 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:
>> 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:
January 02, 2014
i'll answer in code
http://dpaste.dzfl.pl/2bb1a1a8
« First   ‹ Prev
1 2