473,387 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Question about (double *)NULL

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?

--
Rouben Rostamian
Apr 30 '06 #1
5 2812
Rouben Rostamian a écrit :
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?


You are right. The only difference is that you waste code space in a
useless assignment, and that afterwards you write NULL in lowercase.

A
#define null NULL
would accomplish the same thing

Apr 30 '06 #2
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.
Apr 30 '06 #3
On 2006-04-30, Rouben Rostamian <ro****@pc18.math.umbc.edu> wrote:
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?


The cast is not needed in C (but is in C++).

If the package was originally written before C had function prototypes,
then the sample usage (with arguments of type double *) was needed for
compilers that used different representations for pointers to different
types. With function prototypes, the compiler, if necessary, converts
arguments with a value of NULL to the null pointer of the type of the
corresponding function parameter.
Apr 30 '06 #4
On Sun, 30 Apr 2006 17:28:50 -0000, "A. Bolmarcich"
<ag*****@earl-grey.cloud9.net> wrote in comp.lang.c:
On 2006-04-30, Rouben Rostamian <ro****@pc18.math.umbc.edu> wrote:
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?


The cast is not needed in C (but is in C++).


No it is not.

<OT>
The C++ standard requires that the macro NULL be defined as an integer
constant expression that evaluates to a value of 0. It does not allow
it to be defined as such an expression cast to void *, as C does.
</OT>

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
May 1 '06 #5
Rouben Rostamian wrote:
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) ;
}

My question is: Does the cast in (double *)NULL accomplish anything?
No.
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?


It's the same if umfpack_di_symbolic is actually prototyped before its
use.
If the function is not prototyped, then under C90 it will have an
implicit
declaration of...

int umfpack_di_symbolic();

The absense of a prototype would mean that NULL and (double *) NULL
might be different types and/or have different representations when
passed
as an argument. In either case, such a call would invoke undefined
behaviour.

With a prototype in scope the conversion from NULL to (double *) NULL
is implicit.

C99 removed implicit function declarations, but it did not add the
requirement
that a named function be prototyped before use.

Sensible programmers will turn on the 'require function prototypes'
option
on their compilers (if the compiler has one).

--
Peter

May 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: pedicini | last post by:
I work with many dynamically allocated variables in my program including double **, int *, char *. For ex: double **d; d = new (double *) ; for(int i = 0; i < 10; i++) { d = new double; }
7
by: Master of C++ | last post by:
Hello Folks, I have a programming dilemma with templates and "genericity". I can think of a few solutions, but I am not sure which one is the best in terms of genetic C++ style. Any suggestions...
5
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
7
by: Peter Proost | last post by:
Hi, I'm creating an import module to read data from old textfiles, run some calculations on them and save them to sql server. I've figured out how to do a select on textfile using a schema.ini file...
7
by: Mischa | last post by:
Hello, I am trying to use realloc multiple times to extend an array of doubles but unfortunatly it keeps failing. I think I am mixing up the size to which the old memory block needs to be...
2
by: Rouben Rostamian | last post by:
The main() function in the following code defines an m by n matrix, assigns value(s) to its elements, then passes the matrix to function foo(). For whatever it's worth, I have declared foo() so...
5
by: tjay | last post by:
Hi. I wrote some code using sprintf and atof to store a double as a string of fixed length and to convert it back to a double variable. The string is stored in a char buffer global variable. I'm...
0
by: taquito | last post by:
I just started to learn C. This is a very simple question, so please forgive me for posting this here. I have a set of data in, let's say, .txt file. this txt file is in tab-delimited, like 1 4...
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.