Re: Question about (double *)NULL



Rouben Rostamian schrieb:
Umfpack is a C library for computations dealing with sparse
matrices. Several examples in the User's Guide use a certain
cast that puzzles me. Here is an example.

The prototype of the function umfpack_di_symbolic() is:

int umfpack_di_symbolic
(
int n_row,
int n_col,
const int Ap [ ],
const int Ai [ ],
const double Ax [ ],
void **Symbolic,
const double Control [UMFPACK_CONTROL],
double Info [UMFPACK_INFO]
);

The header file umfpack.h has:
#define UMFPACK_CONTROL 20

Here is a sample usage:

int main (void)
{
double *null = (double *) NULL ;
...
(void) umfpack_di_symbolic (n, n, Ap, Ai, Ax, &Symbolic, null, null) ;
...
return (0) ;
}

(Code fragments are put here by "cut-and-paste"ing from the manual.)

My question is: Does the cast in (double *)NULL accomplish anything?
It seems to me that the call to umfpack_di_symbolic() equivalent to:

umfpack_di_symbolic (n, n, Ap, Ai, Ax, &Symbolic, NULL, NULL) ;

Comments?

Educated guess:
It proactively shuts up a C++ compiler abused to compile C...

,---
$ cat doublecast.c
int main (void)
{
double *foo = (void *)0;

return 0;
}

$ gcc -std=c89 -pedantic -Wall -O doublecast.c -c
doublecast.c: In function `main':
doublecast.c:3: warning: unused variable `foo'

$ g++ -std=c++98 -pedantic -Wall -O doublecast.c -c
doublecast.c: In function `int main()':
doublecast.c:3: error: invalid conversion from `void*' to `double*'
doublecast.c:3: warning: unused variable 'foo'
`---
<OT>The correct C++ solution is to use "0" instead of "NULL".</OT>

As most library headers nowadays test whether C or C++ is the
language mode they are processed with, you usually have NULL
defined to be 0 for C++ and (void *)0 for C.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
.



Relevant Pages