Thread overview | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 01, 2014 How to handle char* to string from C functions? | ||||
---|---|---|---|---|
| ||||
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 Re: How to handle char* to string from C functions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | 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 Re: How to handle char* to string from C functions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | 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 Re: How to handle char* to string from C functions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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 Re: How to handle char* to string from C functions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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 Re: How to handle char* to string from C functions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | 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 Re: How to handle char* to string from C functions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | You are going to need the length of your c char*[] then a for-loop should do it :D |
January 02, 2014 Re: How to handle char* to string from C functions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 Re: How to handle char* to string from C functions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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 Re: How to handle char* to string from C functions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | i'll answer in code http://dpaste.dzfl.pl/2bb1a1a8 |
Copyright © 1999-2021 by the D Language Foundation