passing 0 to const char[]

Simen Kjaeraas simen.kjaras at gmail.com
Tue Aug 14 13:09:46 PDT 2012


On Tue, 14 Aug 2012 21:27:25 +0200, Andrew <andrew.spott at gmail.com> wrote:

> I'm trying to interface with C code, where I have a function
> definition that takes two const char[]'s:
>
> PetscErrorCode PetscInitialize(int*, char***, const char[], const
> char[]);
>
> However, the typical way that you pass "Null" values instead of
> the last two arguments is "PETSC_NULL"
>
> The problem is that PETSC_NULL is a macro defined to be 0.  I
> can't do the normal "int PETSC_NULL = 0" thing, because it
> doesn't follow the type signature of PetscInitialize.
>
> How to I pass 0 in place of the last two arguments?  cast(const
> char[])(0) doesn't work, and just an empty string ("\0") doesn't
> work either.
>
> -Andrew

I'm a tad confused. Why can't you just pass null? In D, the literal
0 is not implicitly convertible to void*, char***, or any other
pointer or reference type, so instead you should use the literal
null, which has no nameable type, but is implicitly converted to
any pointer or reference type you may wish.

If you really want to, you can have

enum PETSC_NULL = null;

so that you can write PetscInitialize( &i, PETSC_NULL, PETSC_NULL ),
but I'd recommend just using the null we have.

-- 
Simen


More information about the Digitalmars-d-learn mailing list