473,725 Members | 2,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2612
"LaEisem" <la*****@aol.co mnojunk> 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.com nojunk (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*******@free net.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****@jdyallo p.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*******@free net.de)
Nov 13 '05 #6
In <pa************ *************** *@yahoo.com> Sheldon Simms <sh**********@y ahoo.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**********@y ahoo.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.st rath.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*******@free net.de)
Nov 13 '05 #10

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

Similar topics

28
2865
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 below: The following C program segfaults of IA-64, but works fine on IA-32. int main() { int* p; p = (int*)malloc(sizeof(int));
16
5842
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 compiles fine. What is the warning shown ?
4
10716
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 ASCII characters in the 0-127 range, although I had thought that I might want to use the 128-255 range for special symbols or foreign character codes. This all worked OK for a long time, but a recent update to the compiler on my system has...
9
6261
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 two different ways to handle this table. CASE 1 : create a struct that encaplusates table "Customers" columns public struct structCustomers { public string CustomerID;
9
7133
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 when I tried the program on the Win98 system it will be running on, I get the following error: Cast from string "2076719" to type 'Long' is not valid I am not sure why I only get this error on the Win98 system or how to go about correcting...
10
1654
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 GetDetails(ByVal x As String, ByVal y as String) Dim conn As New SqlConnection ("server=localhost;uid=blah;pwd=blah;database=test") Dim strSQL As String Dim ds As New DataSet()
3
2012
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 & " " & CInt(arrRuleList(i).LeadMemberValue)) Then where arrRuleList(i).operator = ">"
14
2258
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 memory to b,using "malloc" b= (???) malloc( 10*sizeof ( *b ) ) Here,if I want to convert the type by force, which shall I use? I use: b=(int**)malloc(10*sizeof(*b)); the code can compile, but there is a warring "assignment from
1
5971
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? void Print(queue *q) { int j; if ((q->tail-q->head) >=5)
0
8889
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9179
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9116
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8099
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.