473,385 Members | 1,341 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,385 software developers and data experts.

About FAQ 5.4


http://www.eskimo.com/~scs/C-faq/q5.4.html

It says "In particular, a cast may still be necessary before NULL

(as before 0) in a function call argument."

Does that mean if I want to pass a NULL to a function,

I should write like this

foo( (char *)NULL ); /* Assume foo receive a pointer to char */
| Modify | Tue, 14 Oct 2003 22:10:40 +0800 (CST)
Nov 13 '05 #1
7 1584
I wish wrote:

http://www.eskimo.com/~scs/C-faq/q5.4.html

It says "In particular, a cast may still be necessary before NULL

(as before 0) in a function call argument."

Does that mean if I want to pass a NULL to a function,

I should write like this

foo( (char *)NULL ); /* Assume foo receive a pointer to char */


There are essentially two cases: One where the compiler
already knows what kind of pointer foo() expects, and one
where it does not know. The latter has two sub-cases:

1. If a prototype for foo() is in scope at the point of
the call, the compiler knows that foo() expects to
receive a `char*' (or a `double*' or `struct x*' or
whatever). In this case, the compiler arranges to
have NULL converted to the desired type automatically;
the cast is unnecessary but harmless.

2a. If there is no prototype for foo() the compiler does
not know what kind of argument is expected. In this
case, the supplied argument is handled according to
the "default argument promotions," which may or may
not produce the correct result. For example, if NULL
is defined as an unadorned `0', the compiler will pass
the argument as an `int' and not as a `char*'. In
this instance, the cast is required.

2b. If a prototype for foo() is in scope but foo() is a
variadic function and the argument in question is one
of the `...' optional arguments, once again the compiler
doesn't know what kind of argument is expected. For the
fixed arguments before the `...' the situation is as in
case [1], but the `...' arguments are handled as in [2a]:
the default argument promotions prevail, and you need
an explicit cast to override them.

--
Er*********@sun.com
Nov 13 '05 #2
I wish wrote:
It says "In particular, a cast may still be necessary before NULL (as
before 0) in a function call argument."

Does that mean if I want to pass a NULL to a function, I should write
like this

foo( (char *)NULL ); /* Assume foo receive a pointer to char */


This is addressed in question 5.2:

http://www.eskimo.com/~scs/C-faq/q5.2.html

The gist of it is you have to use a cast only when a) there is no function
prototype in scope, or b) the argument is part of a variable-length argument
list.

Note that the author (Steve Summit) recommends you cast NULL whenever you
pass it to a function. He lists a few reasons for saying that. I see his
point, but I don't think I'd want to adopt that practice for my own code.
In my opinion, unnecessary casts should be avoided, because they decrease
the legibility of your code, and could hide programming mistakes.

--
Russell Hanneken
rg********@pobox.com
Remove the 'g' from my address to send me mail.
Nov 13 '05 #3
In <48********@shepjeng.twbbs.org> GA************@shepjeng.twbbs.org (I wish) writes:

http://www.eskimo.com/~scs/C-faq/q5.4.html

It says "In particular, a cast may still be necessary before NULL
(as before 0) in a function call argument."

Does that mean if I want to pass a NULL to a function,
I should write like this

foo( (char *)NULL ); /* Assume foo receive a pointer to char */


ONLY if you don't have a prototype for foo() in scope. Since there is no
good reason for not properly declaring the functions you're using, the
only real case when you have to use the cast is when calling variadic
functions: the compiler has no information about the expected type, so it
cannot perform the conversion itself.

Typical example:

#include <stdio.h>
....
printf("%p\n", (void *)NULL);

OTOH,

fflush(NULL);

is just fine, because the compiler has the prototype of fflush() in scope
and it can perform the conversion itself.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #4
GA************@shepjeng.twbbs.org (I wish) wrote:
http://www.eskimo.com/~scs/C-faq/q5.4.html

It says "In particular, a cast may still be necessary before NULL
(as before 0) in a function call argument."

Does that mean if I want to pass a NULL to a function,
I should write like this

foo( (char *)NULL ); /* Assume foo receive a pointer to char */


Well, that's debatable.

If you have a decent prototype[1] in scope, and that prototype tells you
that the function is not for a variadic function (or that the pointer is
one of the fixed arguments to a variadic function, not part of the
variable arguments), then no, you don't need to.
If you don't have a prototype in scope, you need to, because then the
compiler can't figure out what type of pointer to convert the null
pointer constant to - it doesn't know what type is required unless the
prototype tells it.
If you the null pointer is one of the variable arguments to a variadic
function, you also need to cast, because the compiler can't figure out
what type of pointer is needed - it can in the fixed arguments, of
course, but there's surprisingly little type information in "...".

The above is all clear and not really debatable. The debate starts when
you ask whether, even though you usually don't _need_ to cast a null
pointer argument, you _should_ do so for stylistic reasons.
On one side of the argument, if you simply cast all null pointer
arguments, you don't need to think about it, and you won't forget to
cast one you must cast.
On the other side, you need to know about the difference between
variadic functions and normal ones anyway, and you shouldn't (and in
C99, can't) call functions without a proper declaration in the first
place; and any superfluous cast is a hinder to the legibility of your
code.
Me, I favour the second side, and only use properly prototyped functions
whenever I can help it, and cast only null pointer constants passed as
variadic arguments. But I don't pretend this is an immutable rule -
unless you're me.

Richard

[1] Or complete old-style definition, I presume; but just don't use
old-style definitions, it's safer. An old-style _declaration_ is not
good enough, in any case. (And the reason I say "I presume" is that
I'm not sure; and don't care to find out, because old-style
definitions are a Bad Thing anyway.)
Nov 13 '05 #5
GA************@shepjeng.twbbs.org (I wish) wrote:
http://www.eskimo.com/~scs/C-faq/q5.4.html

It says "In particular, a cast may still be necessary before NULL

(as before 0) in a function call argument."

Does that mean if I want to pass a NULL to a function,

I should write like this

foo( (char *)NULL ); /* Assume foo receive a pointer to char */
Only if there is no prototype for foo in scope or foo is a variadic
function[1], compare FAQ q5.2, as q5.4 suggests: "The table under
question 5.2 above applies for NULL as well as 0 (an unadorned NULL is
equivalent to an unadorned 0)."

[1] Example: you have to explicitly cast a pointer argument to void* for
evaluation using the "%p" printf format specifier.
| Modify | Tue, 14 Oct 2003 22:10:40 +0800 (CST)


Please drop this escape sequence nonsense, or at least put it after a
valid signature delimiter. Thank you.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #6
Irrwahn Grausewitz <ir*******@freenet.de> scribbled the following:
GA************@shepjeng.twbbs.org (I wish) wrote:
| Modify | Tue, 14 Oct 2003 22:10:40 +0800 (CST)
Please drop this escape sequence nonsense, or at least put it after a
valid signature delimiter. Thank you.


No. Please just drop the escape sequence crap. This is Usenet, not a
BBS. You can't count on everyone having an MS-DOS relic in their
newsreader.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"It was, er, quite bookish."
- Horace Boothroyd
Nov 13 '05 #7
I wish <GA************@shepjeng.twbbs.org> wrote:

http://www.eskimo.com/~scs/C-faq/q5.4.html

It says "In particular, a cast may still be necessary before NULL

(as before 0) in a function call argument."

Does that mean if I want to pass a NULL to a function,

I should write like this

foo( (char *)NULL ); /* Assume foo receive a pointer to char */


As others have said, if there is no way for the compiler to know the
intended type (e.g. no prototype, or varargs), then the default promotions
are used which can have undesirable consequences. I was bitten by this not
too long ago, believe it or not (using the comma operator, though). Read
this thread for a real world scenario:

http://groups.google.com/groups?th=c18bfdde21e11e83

The short story is that Linux (or glibc) does:

#define NULL (void *)0

whereas *BSD does:

#define NULL 0

Nov 13 '05 #8

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

Similar topics

1
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
8
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
77
by: nospam | last post by:
Reasons for a 3-tier achitecture for the WEB? (NOTE: I said, WEB, NOT WINDOWS. DON'T shoot your mouth off if you don't understand the difference.) I hear only one reason and that's to switch a...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
5
by: eScrewDotCom | last post by:
www.eScrew.com eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
0
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
7
by: Edward Yang | last post by:
A few days ago I started a thread "I think C# is forcing us to write more (redundant) code" and got many replies (more than what I had expected). But after reading all the replies I think my...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.