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

Is a "cast" ever necessary in "return"?

On-the-job, I have "inherited" a lot of old C language software.
A question or two about when "casting" of null pointer constants
is [not] needed has occurred during behind-the-scenes cleanup
of some of that software. That subject seems not to be addressed,
at least not directly, in the C FAQ where FAQ 5.2 seems most relevant.

References:
* C FAQ 5.2 Null pointers
(Including conditions where "casting" of
null pointer constants is (not) needed)
* ANSI C Standard 6.2.2.3 Pointers
(Including null pointer constant)
* ANSI C Standard 6.6.6.4 The return statement
* ANSI C Standard 7.1.6 (Including NULL macro which expands to
a null pointer constant)

Is it ever necessary to "cast", in a "return" statement, a NULL
(or an "unadorned 0", to use terminology from C FAQ 5.2)
that appears as the to-be-returned value in that "return" statement?
Does it matter whether or not a function prototype is in scope
for the function in which such a "return" statement appears?

Example: If the "return" statement below appeared in a C function,
would the "(char *)" cast ever be necessary?
return (char *)NULL;

Does the return of NULL or of an "unadorned 0" in a "return" statement
constitute an "assignment context", as that term is used in C FAQ 5.2?
In general, does the return of an arbitrary value in a "return"
statement constitute such an "assignment context"?
If it did, would that not be a problem if an unadorned 0 (not NULL)
was to be returned in a "return" statement with the intent that it be
interpreted as an integer 0 rather than as a (null) pointer?

Much thanks...

Nov 13 '05 #1
10 2573
"LaEisem" <la*****@aol.comnojunk> wrote
....
Is it ever necessary to "cast", in a "return" statement, a NULL
(or an "unadorned 0", to use terminology from C FAQ 5.2)
that appears as the to-be-returned value in that "return" statement?
Does it matter whether or not a function prototype is in scope
for the function in which such a "return" statement appears?

Example: If the "return" statement below appeared in a C function,
would the "(char *)" cast ever be necessary?
return (char *)NULL;

Does the return of NULL or of an "unadorned 0" in a "return" statement
constitute an "assignment context", as that term is used in C FAQ 5.2?
In general, does the return of an arbitrary value in a "return"
statement constitute such an "assignment context"?
Try answering this yourself: what if the function returned, for example,
a double and you used 'return 42;'? What would get returned? Why would
be returning NULL (or 0, for that matter) different?
If it did, would that not be a problem if an unadorned 0 (not NULL)
was to be returned in a "return" statement with the intent that it be
interpreted as an integer 0 rather than as a (null) pointer?


NULL is on many platforms #defined as 0.

Peter
Nov 13 '05 #2
la*****@aol.comnojunk (LaEisem) wrote:

<snip>
Is it ever necessary to "cast", in a "return" statement, a NULL
(or an "unadorned 0", to use terminology from C FAQ 5.2)
that appears as the to-be-returned value in that "return" statement?
Does it matter whether or not a function prototype is in scope
for the function in which such a "return" statement appears?
As a function definition is also a proper function prototype there is
no way to define a function returning a pointer without a prototype in
scope.
Example: If the "return" statement below appeared in a C function,
would the "(char *)" cast ever be necessary?
return (char *)NULL;
If the function is defined to return a pointer to char the cast is
spurious. Otherwise the cast makes no sense. Just drop it.
Does the return of NULL or of an "unadorned 0" in a "return" statement
constitute an "assignment context", as that term is used in C FAQ 5.2?
In general, does the return of an arbitrary value in a "return"
statement constitute such an "assignment context"?
Generally, yes.
If it did, would that not be a problem if an unadorned 0 (not NULL)
was to be returned in a "return" statement with the intent that it be
interpreted as an integer 0 rather than as a (null) pointer?


Any integer constant resolving to 0 or such a constant casted to void*
serves as a proper null pointer constant. Thus it will automatically be
casted to the pointer type in question on assignment. NULL and 0 are
interchangeable in pointer contexts (with the exceptions mentioned in
FAQ 5.2, but you already know this).

HTH
Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #3
Irrwahn Grausewitz wrote:
As a function definition is also a proper function prototype there is
no way to define a function returning a pointer without a prototype in
scope.


Not all function definitions are prototypes. The following is a
definition but not a prototype:

int *f() { return 0; }

Jeremy.
Nov 13 '05 #4
On Tue, 28 Oct 2003 23:11:22 +0000, LaEisem wrote:
Is it ever necessary to "cast", in a "return" statement, a NULL
(or an "unadorned 0", to use terminology from C FAQ 5.2)
that appears as the to-be-returned value in that "return" statement?
Does it matter whether or not a function prototype is in scope
for the function in which such a "return" statement appears?


I think this is pretty clearly explained in 6.8.6.4:

#3 If a return statement with an expression is executed, the
value of the expression is returned to the caller as the value
of the function call expression. If the expression has a type
different from the return type of the function in which it
appears, the value is converted as if by assignment to an
object having the return type of the function.

A function prototype isn't necessary to determine the return
type of the function because the return statement is *in* the
function definition and the return type is known.

As long as a function is declared to return any pointer type,
there is no need to cast the null pointer constant 0 or the
macro NULL in a return statement. The only case in which you
might need to cast either of the above would in really ancient
code that intends to return a pointer value from a function
declared without a return type at all. In that case, the return
type would be recognized as int, and the cast would be
necessary -- but the code still wouldn't be correct, because
there is no guarantee that a null pointer can be converted to
an int (and presumably back to a pointer) without changing
its value.

Since such function declarations are not even legal C anymore
anyway, the fix would be to change the function declarations
to use an explicit return type.

-Sheldon

Nov 13 '05 #5
Jeremy Yallop <je****@jdyallop.freeserve.co.uk> wrote:
Irrwahn Grausewitz wrote:
As a function definition is also a proper function prototype there is
no way to define a function returning a pointer without a prototype in
scope.


Not all function definitions are prototypes. The following is a
definition but not a prototype:

int *f() { return 0; }


Doh! You're right. But I think my point holds that you cannot
(portably) define a function returning a pointer without, err...
defining that function to return a pointer.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #6
In <pa****************************@yahoo.com> Sheldon Simms <sh**********@yahoo.com> writes:
As long as a function is declared to return any pointer type,
there is no need to cast the null pointer constant 0 or the
macro NULL in a return statement. The only case in which you
might need to cast either of the above would in really ancient
code that intends to return a pointer value from a function
declared without a return type at all. In that case, the return
type would be recognized as int, and the cast would be
necessary
If you put the cast, the compiler *must* emit a diagnostic, because it
has to convert the pointer back to int, and this requires an *explicit*
cast from pointer to int.
-- but the code still wouldn't be correct, because
there is no guarantee that a null pointer can be converted to
an int (and presumably back to a pointer) without changing
its value.
The code may not compile at all, given that it requires a diagnostic.
Since such function declarations are not even legal C anymore
anyway, the fix would be to change the function declarations
to use an explicit return type.


Such functions are in *no* way different from functions explicitly
defined as returning int (in both C89 and pre-ANSI C). So, I can't
understand why you think that they are somehow a special case in the
context of this discussion. They aren't: a function defined as returning
int is a function defined as returning int, whether the definition was
explicit or implicit.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #7
On Wed, 29 Oct 2003 10:53:47 +0000, Dan Pop wrote:
In <pa****************************@yahoo.com> Sheldon Simms <sh**********@yahoo.com> writes:
Since such function declarations are not even legal C anymore
anyway, the fix would be to change the function declarations
to use an explicit return type.


Such functions are in *no* way different from functions explicitly
defined as returning int (in both C89 and pre-ANSI C). So, I can't
understand why you think that they are somehow a special case in the
context of this discussion.


I didn't, I was thinking that it is possible that some beginners
might not know that the return value is (implicitly) int when
confronted with such a function - especially if the function
body contains something like "return (char *)NULL;"

-Sheldon

Nov 13 '05 #8
Irrwahn Grausewitz wrote:
Doh! You're right. But I think my point holds that you cannot
(portably) define a function returning a pointer without, err...
defining that function to return a pointer.


Just as you can't eat a banana without eating a banana.

--
Thomas.

Nov 13 '05 #9
Thomas Stegen <ts*****@cis.strath.ac.uk> wrote:
Irrwahn Grausewitz wrote:
[...] you cannot
(portably) define a function returning a pointer without, err...
defining that function to return a pointer.


Just as you can't eat a banana without eating a banana.


Exactely.
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #10
j

"Jeremy Yallop" <je****@jdyallop.freeserve.co.uk> wrote in message
news:sl*******************@ekoi.cl.cam.ac.uk...
Irrwahn Grausewitz wrote:
As a function definition is also a proper function prototype there is
no way to define a function returning a pointer without a prototype in
scope.
Not all function definitions are prototypes. The following is a
definition but not a prototype:

int *f() { return 0; }


And it is also considered obsolescent.
Jeremy.

Nov 13 '05 #11

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

Similar topics

28
by: Robert Gamble | last post by:
I was taking a look at some of the C puzzles at: http://purana.csa.iisc.ernet.in/~gkumar/cquestions.html and have not had any trouble with any of them except for the first one which is reproduced...
16
by: jose_luis_fdez_diaz_news | last post by:
Hi, If I don't include <libgen.h> I get then warnig below in regcmp call: warning: improper pointer/integer combination: op "=" but if I include it the warning is not shown, but them program...
4
by: John Devereux | last post by:
Hi, I would like some advice on whether I should be using plain "chars" for strings. I have instead been using "unsigned char" in my code (for embedded systems). In general the strings contain...
9
by: Hasan O. Zavalsiz | last post by:
Hi , i am trying to figure out which approach is better to use . let me explain the scenario. i am using the "Nortwind" database . in this database i have "Customers " table .The following is the...
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
10
by: roy.anderson | last post by:
Error is thus: "Cast from type 'DBNull' to type 'String' is not valid." Simple, right? Well, no (or at least not for me). Here's a function, followed by the calling code below: Function...
3
by: hazz | last post by:
I get an InvalidCastException "Cast from string "6<5" to type 'Boolean is not valid. for If (CInt(objBuyInfo.Budget) & " " & arrRuleList(i).operator & " " &...
14
by: Alex | last post by:
I saw the topic of "wired code " about "point to array" and know a little about it. But I am still confused about the question below: I define a point to array "b" int (*b); then I locate the...
1
by: lavender | last post by:
when I compile my code, it show : WARNING : passing `int' to argument 1 of `deleteQueue(itemType *, queue *)' lacks a cast What is the meaning for this warning? Where I should coorect in my code?...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.