Convert string[] to char**

Bruce Smith b.e.smith at computer.org
Tue Jun 4 05:43:48 PDT 2013


The C function's signature is determined by the library from which it 
originates; the D signature created by SWIG is tied to the C original; 
therefore I can't alter the fact that the parameter is of type char** 
(i.e. it is mutable).

OTOH, toStringz produces an immutable string.

Bruce

On 04/06/13 22:07, David wrote:
> Am 04.06.2013 13:49, schrieb Bruce Smith:
>> Thank you Jacob and bearophile ... just had to add a cast to get rid of
>> immutable from your suggestion. For completeness, this compiles under
>> 2.063:
>>
>>      void foo(char** k)
>>      {
>>          // body
>>      }
>>
>>      string[] mykeys;
>>      auto ckeys = cast(char**)mykeys.map!toStringz.array;
>>
>>      foo(ckeys);
>>
>> Bruce
>>
>> On 04/06/13 20:59, Jacob Carlborg wrote:
>>> On 2013-06-04 12:41, bearophile wrote:
>>>
>>>> An alternative is something like (untested):
>>>>
>>>> auto ckeys = mykeys.map!toStringz.array;
>>> Right, that should work.
>>>
> Casting it isn't the best way here, you shouldn't cast immutability
> away, make sure your function does not mutate it (it must not!) then you
> could change the function signature to const(void*)*, otherwise use
> char[] instead of string, this should do it:
>
> mykeys.map!(x => x.dup.toStringz).array
>
>



More information about the Digitalmars-d mailing list