473,402 Members | 2,053 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,402 software developers and data experts.

does a cast become a null pointer ?

MC
Hi
If I have a pointer to a some structure
say for example payroll_ptr where
struct payroll { ... } has some members and
if i use a function argument as

int function_process ( (payroll_ptr) 0 , ..)
does the first argument become a NULL
pointer ?

Thanks
Mahesh
Nov 13 '05 #1
5 2564

"MC" <ma*********@yahoo.com> wrote in message
news:bi**********@zcars0v6.ca.nortel.com...
Hi
If I have a pointer to a some structure
say for example payroll_ptr where
struct payroll { ... } has some members and
if i use a function argument as

int function_process ( (payroll_ptr) 0 , ..)
does the first argument become a NULL
pointer ?

Thanks
Mahesh


Yes, the first parameter will contain value 0 (null pointer). Pointers that
contain the value of 0, are all NULL pointers.

All pointers can assign and test for value 0 (but to not run into explicit
casting problems, the NULL macro has the type pointer-to-void).
Nov 13 '05 #2
On Wed, 27 Aug 2003 12:33:58 -0700, "MC" <ma*********@yahoo.com>
wrote:
Hi
If I have a pointer to a some structure
say for example payroll_ptr where
struct payroll { ... } has some members and
if i use a function argument as

int function_process ( (payroll_ptr) 0 , ..)
does the first argument become a NULL
pointer ?


Only if payroll_ptr is the type (via a typedef) of the pointer and not
the pointer itself. Otherwise you have a syntax error.

If their is a prototype in scope for the function then the cast is
completely superfluous.

If you want to pass a null pointer constant to a function, it would
make the code much easier to read if you simply used NULL.
<<Remove the del for email>>
Nov 13 '05 #3
"MC" <ma*********@yahoo.com> wrote in message news:<bi**********@zcars0v6.ca.nortel.com>...
If I have a pointer to a some structure
say for example payroll_ptr where
struct payroll { ... } has some members and
if i use a function argument as

int function_process ( (payroll_ptr) 0 , ..)
does the first argument become a NULL
pointer ?


It's better C to not have the cast. Modern C makes a clear distinction
between integers and pointers. (char*)0 is an integer converted to a
pointer; the compiler treats it the same as (char*)1. So it's not a
null pointer; it's a nulled pointer. The practical difference is nil,
but that doesn't nullify the language difference.

Sam
Nov 13 '05 #4
Samuel Barber wrote:
(char*)0 is an integer converted to a
pointer; the compiler treats it the same as (char*)1.
No, it doesn't treat them the same by any means.
So it's not a null pointer; it's a nulled pointer.
0 is a null pointer constant.
(char*)0, is a null pointer.
(char*)1, is the integer number one, cast to type pointer to char.
The practical difference is nil,
but that doesn't nullify the language difference.


"null pointer" is a technical term in C.
"nulled pointer" is a term that you just made up.
I see that one difference as being substantial.

Whether or not (char*)1 means anything at all,
is implementation defined.
The meaning of ((char*)0) is defined by the standard.

--
pete
Nov 13 '05 #5
MC wrote:

"Barry Schwarz" <sc******@deloz.net> wrote in message
news:bi**********@216.39.134.219...
If their is a prototype in scope for the function then the cast is
completely superfluous.


So if I have a header file that has the prototype
for the function and I include that header file
in this source file, then the cast is superfluous .
Am i right ?


In the case presented (where question refers to the
function's first argument), yes.

There's a wrinkle, though, for what are called "variadic
functions." Like the *printf() family, these functions take
a few "mandatory" arguments of known types followed by an
indeterminate number of "variable" arguments. Both the
number and the types of the variable arguments can differ
from one call to the next, e.g.

printf ("Hello, world!\n");
fprintf (stdout, "The answer is %d\n", 42);
sprintf (buff, "%d / %d = %f", 42, 26, 42.0/26.0);

Now, how does all this bear on your question? The
catch is that the compiler has no idea what types the
variable arguments are supposed to have: they can, as
shown above, be different in different calls. Thus, the
compiler does not know it's supposed to convert an argument
from whatever type you present to whatever type the function
actually expects; the compiler doesn't know the expected
type. So for arguments that correspond to the "..." part
of a variadic function's parameter list, casts are still
necessary if the supplied argument doesn't already have
the correct type.

To sum up, if the type of the supplied value doesn't
match the type of the expected argument:

- If there is no prototype in scope, you need a cast.
(Better still would be to introduce a prototype.)

- If there is a prototype in scope and the function
is not variadic, no cast is needed because the
compiler provides the needed conversion. (But if
there's an explicit cast, no harm is done.)

- If there's a prototype and the function is variadic
but the supplied value is for one of the "fixed"
arguments, no cast is needed. (That's why the
first argument is special: there must be at least
one fixed argument, so the very first argument is
necessarily fixed.)

- If there's a prototype and the function is variadic
and the supplied value is for one of the "..."
arguments, you need a cast.

Complicated as all this may seem, it *still* isn't quite
accurate. When the compiler doesn't know what the function
expects (no prototype, or "..." argument), it applies the
"argument promotions." These will, for example, convert a
`float' value to `double' automatically, so some cases of
type mismatch don't actually need the explicit cast mentioned
above.

In light of all these ins and outs, Henry Spencer recommends
explicit casts even when they don't seem to be needed; see the
Third Commandment in

http://www.lysator.liu.se/c/ten-commandments.html

Personally, I confess myself a habitual and wilful sinner against
the Third Commandment, and I will no doubt burn in Hell (or in
gdb, which some people consider different.) But it is probably a
good idea to consider the Commandments carefully before deciding
to violate them: if you're going to sin, do so on purpose and not
by accident.

--
Er*********@sun.com
Nov 13 '05 #6

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

Similar topics

2
by: Devrobcom | last post by:
Hi I know that this is not the Lint forum, but I get some Lint warnings when setting my pointers to NULL. And the problem is I don't know howto cast a function ptr the right way. typedef int...
10
by: LaEisem | last post by:
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 needed has occurred during behind-the-scenes cleanup of some...
4
by: William Payne | last post by:
Hello! If a cast from a void pointer to a pointer to a struct fails at runtime, is NULL the result? I.E, is this code meaningful: struct my_struct_t foo* = (struct my_struct_t*)void_pointer; //...
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
74
by: Suyog_Linux | last post by:
I wish to know how the free()function knows how much memory to be freed as we only give pointer to allocated memory as an argument to free(). Does system use an internal variable to store allocated...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
9
by: Frederick Gotham | last post by:
Let's assume that we're working on the following system: CHAR_BIT == 8 sizeof( char* ) == 4 (i.e. 32-Bit) Furthermore, lets assume that the memory addresses are distributed as follows: ...
42
by: Sheldon | last post by:
Hi, This program works when tested with gdb ( see results 1) but when used in a larger program where 12 becomes 1500 there exists a problem when freeing the memory ( see results 2). Can anyone...
7
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
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
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
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
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...
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.