www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - passing 0 to const char[]

reply "Andrew" <andrew.spott gmail.com> writes:
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
Aug 14 2012
next sibling parent reply "cal" <callumenator gmail.com> writes:
On Tuesday, 14 August 2012 at 19:27:26 UTC, Andrew 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
How about cast(const(char[]))([0]) ?
Aug 14 2012
parent "cal" <callumenator gmail.com> writes:
 How about cast(const(char[]))([0]) ?
Although I think what you actually want is probably just null, since a C function expecting an array is expecting a pointer, and passing cast(const(char[]))([0]) satisfies the signature but gives you a non-null array. I would guess what you really want to alias PETSC_NULL to null and pass that...
Aug 14 2012
prev sibling parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Tue, 14 Aug 2012 21:27:25 +0200, Andrew <andrew.spott 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
Aug 14 2012
parent reply "Andrew Spott" <andrew.spott gmail.com> writes:
On Tuesday, 14 August 2012 at 20:10:13 UTC, Simen Kjaeraas wrote:
 On Tue, 14 Aug 2012 21:27:25 +0200, Andrew 
 <andrew.spott 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.
This appears to be the correct answer. I was mostly just confused. something #defined is just replaced by the C preprocessor (or so I thought), which means that in C, you would be passing 0 to PetscInitialize... which seems weird. passing null seems to work though. -Andrew
Aug 14 2012
parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Tuesday, August 14, 2012 23:28:08 Andrew Spott wrote:
 This appears to be the correct answer. I was mostly just
 confused. something #defined is just replaced by the C
 preprocessor (or so I thought), which means that in C, you would
 be passing 0 to PetscInitialize... which seems weird.
0 _is_ the null value in C. What's weird is that they had a non-standard macro for it (NULL is frequently used, but PETSC_NULL is not standard at all). - Jonathan M Davis
Aug 14 2012
parent "Andrew Spott" <andrew.spott gmail.com> writes:
On Tuesday, 14 August 2012 at 21:33:34 UTC, Jonathan M Davis
wrote:
 On Tuesday, August 14, 2012 23:28:08 Andrew Spott wrote:
 This appears to be the correct answer. I was mostly just
 confused. something #defined is just replaced by the C
 preprocessor (or so I thought), which means that in C, you 
 would
 be passing 0 to PetscInitialize... which seems weird.
0 _is_ the null value in C. What's weird is that they had a non-standard macro for it (NULL is frequently used, but PETSC_NULL is not standard at all). - Jonathan M Davis
There is some explanation in the header. PETSC tries to be as portable as possible, apparently there is some weird cases where 0 *isn't* the default value for NULL.
Aug 14 2012