473,763 Members | 7,541 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

implicit type conversions

Let me see if I got this :)
1. I know the rules for type conversions in arithmetic expressions
2. I know that an implicit type conversion is done at assignment, so
float x = 1.23;
int t = (int) x;
is equivalent to
int t = x;
(could the latter produce a warning on some complier?)
3. I know that implicit conversions take place with function arguments, but
am a bit shaky here. I suppose that passing a char to a function accepting
int or long will always work, and those sort of conversions seem to make
sense. But what sort of things won't work here? For example, the FAQ says
that the NULL pointer *must* be cast to the appropriate type when sent to a
function as an argument (so, for example, time(NULL) is incorrect, and
time( (time_t *) NULL) is correct?), which seems to indicate that
conversions from (void *) to (some_type *) in function argument lists need
not be (isn't) implicit. If someone would clarify this, I'd be most grateful
(like which sort of things cause warnings, which need be cast and the like).

Thank you.
Nov 14 '05 #1
15 2296
buda wrote:
Let me see if I got this :)
1. I know the rules for type conversions in arithmetic expressions
2. I know that an implicit type conversion is done at assignment, so
float x = 1.23;
int t = (int) x;
is equivalent to
int t = x;
(could the latter produce a warning on some complier?)
Yes. The Standard requires diagnostics for some violations,
but does not forbid diagnostics even when no violation has been
detected. Years ago, I used a compiler that complained about

float f = 0.0;

.... because of the potential "loss of precision" involved in
shortening the `double' to `float'.

Sometimes the extra diagnostics are helpful, and sometimes
they are merely noise.
3. I know that implicit conversions take place with function arguments, but
am a bit shaky here. I suppose that passing a char to a function accepting
int or long will always work, and those sort of conversions seem to make
sense. But what sort of things won't work here? For example, the FAQ says
that the NULL pointer *must* be cast to the appropriate type when sent to a
function as an argument (so, for example, time(NULL) is incorrect, and
time( (time_t *) NULL) is correct?), which seems to indicate that
conversions from (void *) to (some_type *) in function argument lists need
not be (isn't) implicit. If someone would clarify this, I'd be most grateful
(like which sort of things cause warnings, which need be cast and the like).


If the function has a prototype that describes the types of
all its formal parameters, the argument values you supply will be
converted to the types the function expects. For example, if you
have #include'd <time.h>, the time() function has been declared
as taking one argument of type `time_t*', so the compiler has the
information it needs to convert a plain `NULL' to `(time_t*)NULL' .
In this situation, time(NULL) and time( (time_t*)NULL ) are
equivalent.

However, there are three situations where the compiler lacks
the information it would need to do such conversions automatically:

- If the function has not been declared prior to its use,
the compiler knows nothing about what parameters it expects.

- If the function has been declared but without a prototype
describing the arguments (e.g., `double trouble();'), the
compiler once again has no knowledge of what's expected.

- If the function has been declared with a prototype but is
a "variadic" function (that is, the prototype ends with
`,...)'), the compiler has no knowledge about the parameters
that correspond to the `...' piece. (It does, of course,
know about the parameters that precede it.)

In these cases, you must either write the conversions explicitly or
accept the "default argument promotions." Since `NULL' can be
either `(void*)0' or plain `0' (or other equivalent forms), you
don't really know exactly what you've written when you write an
unadorned `NULL', so the conversion must be made explicitly.

Recommended practices to ease the pain:

- Always declare functions before using them. (In fact, this
is mandatory under the latest "C99" Standard, and the compiler
is required to complain if you fail to do this.)

- Use argument prototypes in function declarations, so the
compiler knows as much as possible about the function. This
lets it make the proper conversions automatically, and lets
it catch some kinds of mistakes.

- The best way to declare a library function -- from the Standard
library or from any other -- is to #include the appropriate
header.

If you always declare functions and always use prototypes, the
only remaining situation you need to worry about is the arguments
that match the `...' parameters of variadic functions.

--
Er*********@sun .com

Nov 14 '05 #2

On Wed, 2 Jun 2004, buda wrote:

Let me see if I got this :)
1. I know the rules for type conversions in arithmetic expressions
No, probably not. But they're quite simple as long as you stay
away from corner cases. Here's the rule of thumb: Unsigned trumps
signed. Long trumps int. Short and char are automatically promoted
to int (signed or unsigned, depending on the implementation limits).
Void pointers can be converted to and from any other data pointer
type silently and automatically. You can assign between floating-point
and integer types silently (though it's not a good idea IMHO because
it will usually cause lots of spurious compiler warnings). Everything
else probably requires a cast.
For the real rules, read the Standard.
2. I know that an implicit type conversion is done at assignment, so
float x = 1.23;
int t = (int) x;
is equivalent to
int t = x;
(could the latter produce a warning on some complier?)
Sure. On the DS9000, it produces "Warning: This program may or
may not be standard C." More helpfully, most compilers will warn
you about a possible "loss of precision" during the conversion.
Neither warning is required by the Standard.
3. I know that implicit conversions take place with function arguments, but
am a bit shaky here. I suppose that passing a char to a function accepting
int or long will always work, and those sort of conversions seem to make
sense. But what sort of things won't work here?
Passing negative numbers to functions expecting 'unsigned' types
is often dangerous, though well-defined by the Standard. Passing
anything to a variadic function like 'printf' requires careful thought.
For example, the FAQ says
that the NULL pointer *must* be cast to the appropriate type when sent to a
function as an argument (so, for example, time(NULL) is incorrect, and
time( (time_t *) NULL) is correct?),
Wrong. NULL must be cast *only* when passing it to a variadic function
expecting a particular type of pointer. For example,

printf("%p\n", NULL); is obviously wrong if #define NULL 0
printf("%p\n", (void*)NULL); is correct in all cases
which seems to indicate that
conversions from (void *) to (some_type *) in function argument lists need
not be (isn't) implicit. If someone would clarify this, I'd be most grateful
(like which sort of things cause warnings, which need be cast and the like).


(void*) and (some_type*) are mutually convertible whenever 'some_type'
is a data type. (That is, a conversion between (void*) and (int(*)())
requires a cast, and isn't even guaranteed to work at all. This is
because (int(*)()) is a function pointer type, not a data pointer type.)

Read the FAQ again, and then check the standard (Google "N869" for
the last public draft thereof).

HTH,
-Arthur

Nov 14 '05 #3
Thank you, all clear now :)
Nov 14 '05 #4
Eric Sosman wrote:
.... snip ...
- The best way to declare a library function -- from the
Standard library or from any other -- is to #include the
appropriate header.

If you always declare functions and always use prototypes,
the only remaining situation you need to worry about is the
arguments that match the `...' parameters of variadic functions.


So it appears to me that if the system declares NULL as (void *)0
then the code:

printf("%p\n", NULL);

is well defined, but if the system simply declares NULL as 0 there
could be a problem. This is fairly obvious in this case, but may
not be if the pointer variable is passed in. The problem will
_probably_ not arise if the sizes of int and void* are the same.
This could be a sneaky bug, and should encourage always declaring
NULL as (void*)0.

--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison
Nov 14 '05 #5

On Wed, 2 Jun 2004, CBFalconer wrote:
[...]
So it appears to me that if the system declares NULL as (void *)0
then the code:

printf("%p\n", NULL);

is well defined, but if the system simply declares NULL as 0 there
could be a problem. This is fairly obvious in this case, but may
not be if the pointer variable is passed in. The problem will
_probably_ not arise if the sizes of int and void* are the same.
This could be a sneaky bug, and should encourage always declaring
NULL as (void*)0.


s/declaring/defining/, I would think is the correct terminology.
Anyway...

This is a sneaky bug that *only* arises if the programmer passes
an unadorned 'NULL' to a variadic function expecting a pointer.
What's more, the bug is only *fixable* if that variadic function
expects a pointer to void (not any other kind of pointer). In
practice, I bet this boils down to "This sneaky bug appears only
when the programmer calls 'printf("%p", NULL)'", which obviously
never, ever happens except in toy pedagogical programs. Which
aren't really the focus of compiler implementors, I would think.

So, while IMHO it does make more sense to define NULL using the
"more strict" definition of (void*)0 rather than an unadorned 0,
I don't think the otherwise-undefined behavior of a pathological
and non-conforming program is a compelling argument for it. :)

-Arthur
Nov 14 '05 #6

"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> wrote in message
news:Pi******** *************** ************@un ix47.andrew.cmu .edu...

On Wed, 2 Jun 2004, CBFalconer wrote:
[...]
So it appears to me that if the system declares NULL as (void *)0
then the code:

printf("%p\n", NULL);

is well defined, but if the system simply declares NULL as 0 there
could be a problem. This is fairly obvious in this case, but may
not be if the pointer variable is passed in. The problem will
_probably_ not arise if the sizes of int and void* are the same.
This could be a sneaky bug, and should encourage always declaring
NULL as (void*)0.


s/declaring/defining/, I would think is the correct terminology.
Anyway...

This is a sneaky bug that *only* arises if the programmer passes
an unadorned 'NULL' to a variadic function expecting a pointer.
What's more, the bug is only *fixable* if that variadic function
expects a pointer to void (not any other kind of pointer). In
practice, I bet this boils down to "This sneaky bug appears only
when the programmer calls 'printf("%p", NULL)'", which obviously
never, ever happens except in toy pedagogical programs. Which
aren't really the focus of compiler implementors, I would think.

So, while IMHO it does make more sense to define NULL using the
"more strict" definition of (void*)0 rather than an unadorned 0,
I don't think the otherwise-undefined behavior of a pathological
and non-conforming program is a compelling argument for it. :)

-Arthur


what about the case where your variadic function expects NULL to terminate a
list of pointers to void

e.g. (not compiled)

size_t myfunc(void *p1,...)
{
size_t c;
va_list list;
va_start(list,p 1);
c = 0;
while (p1 != NULL)
{
/* do something uninteresting with p1 - actually just count */
c++;
p1 = va_arg(list,voi d *);
}
return c;
}

Nov 14 '05 #7
In <Pi************ *************** ********@unix47 .andrew.cmu.edu > "Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> writes:

On Wed, 2 Jun 2004, CBFalconer wrote:
[...]
So it appears to me that if the system declares NULL as (void *)0
then the code:

printf("%p\n", NULL);

is well defined, but if the system simply declares NULL as 0 there
could be a problem. This is fairly obvious in this case, but may
not be if the pointer variable is passed in. The problem will
_probably_ not arise if the sizes of int and void* are the same.
This could be a sneaky bug, and should encourage always declaring
NULL as (void*)0.


s/declaring/defining/, I would think is the correct terminology.
Anyway...

This is a sneaky bug that *only* arises if the programmer passes
an unadorned 'NULL' to a variadic function expecting a pointer.
What's more, the bug is only *fixable* if that variadic function
expects a pointer to void (not any other kind of pointer). In
practice, I bet this boils down to "This sneaky bug appears only
when the programmer calls 'printf("%p", NULL)'", which obviously
never, ever happens except in toy pedagogical programs. Which
aren't really the focus of compiler implementors, I would think.

So, while IMHO it does make more sense to define NULL using the
"more strict" definition of (void*)0 rather than an unadorned 0,
I don't think the otherwise-undefined behavior of a pathological
and non-conforming program is a compelling argument for it. :)


A much more compelling argument for NULL as (void*)0 is the frequently
seen assumption that NULL can be used anywhere a plain 0 does the job,
e.g. the null character terminating a string (the NULL name is probably
at the root of this confusion).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #8
In <pOxvc.6044$OI5 .3532@edtnps84> "Peter Slootweg" <pe************ @xtxexlxuxs.xnx ext> writes:

"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> wrote in message
news:Pi******* *************** *************@u nix47.andrew.cm u.edu...

On Wed, 2 Jun 2004, CBFalconer wrote:
[...]
> So it appears to me that if the system declares NULL as (void *)0
> then the code:
>
> printf("%p\n", NULL);
>
> is well defined, but if the system simply declares NULL as 0 there
> could be a problem. This is fairly obvious in this case, but may
> not be if the pointer variable is passed in. The problem will
> _probably_ not arise if the sizes of int and void* are the same.
> This could be a sneaky bug, and should encourage always declaring
> NULL as (void*)0.


s/declaring/defining/, I would think is the correct terminology.
Anyway...

This is a sneaky bug that *only* arises if the programmer passes
an unadorned 'NULL' to a variadic function expecting a pointer.
What's more, the bug is only *fixable* if that variadic function
expects a pointer to void (not any other kind of pointer). In
practice, I bet this boils down to "This sneaky bug appears only
when the programmer calls 'printf("%p", NULL)'", which obviously
never, ever happens except in toy pedagogical programs. Which
aren't really the focus of compiler implementors, I would think.

So, while IMHO it does make more sense to define NULL using the
"more strict" definition of (void*)0 rather than an unadorned 0,
I don't think the otherwise-undefined behavior of a pathological
and non-conforming program is a compelling argument for it. :)

what about the case where your variadic function expects NULL to terminate a
list of pointers to void


Use the obvious: (void *)NULL or (void *)0. Anything not equivalent to
one of these forms (or a null void pointer) is a bug.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #9
CBFalconer <cb********@yah oo.com> wrote:
So it appears to me that if the system declares NULL as (void *)0
then the code:

printf("%p\n", NULL);

is well defined, but if the system simply declares NULL as 0 there
could be a problem. This is fairly obvious in this case, but may
not be if the pointer variable is passed in. The problem will
_probably_ not arise if the sizes of int and void* are the same.
It would arise if 0 and (void *)0 have different representations
(which is not that uncommon)
This could be a sneaky bug, and should encourage always declaring
NULL as (void*)0.


There are still problem situations, because (void *) could be a different
size to other pointer types, eg:

void put_strings(cha r *f1, ...)
{
va_list ap;
va_start(ap, f1);
while (f1 != NULL)
{
puts(f1);
f1 = va_arg(ap, char *);
}
va_end(ap);
}

If called with: put_strings("fo o", NULL); would cause UB even if
NULL is (void *)0.
Nov 14 '05 #10

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

Similar topics

3
2618
by: Reneé | last post by:
I wanted to know the order of implicit conversions and which sort of values allow them. From searching around in books and the archive of this mailing list, it seems to be that only numbers are implicitly converted within each other and bools can be implicitly converted to ints? However, I'm unable to find any other implicit conversions and the order of the implicit conversions (something like int->float->long). Any help would be greatly...
9
8516
by: Simon | last post by:
Hi All, Is it possible to disallow implicit casting for an operand of a function written in C? i.e. void foo(int a) {..} short b; foo(b) // error without explicit cast
11
7622
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type Test. I expected the same error from the following line, but it compiles fine. The double is silently truncated to an int and then fed in to the implicit conversion operator. Why does this happen? Is there any way that I can keep the implicit...
9
2085
by: Girish | last post by:
Im trying to understand implicit type conversions from object -> string and vice versa. I have two classes, one Driver and one called StringWrapper. These are just test classes that try and emulate the pattern im trying to follow in an existing project. These are my steps: 1) I have a method "printMe" existing in the application which originally used to take in a string. This method is static and sits in the Driver
11
12794
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
36
3636
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work in a C# program but not VB? -- Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/ "Programming is an art form that fights back"
17
2512
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
82
4626
by: robert bristow-johnson | last post by:
here is a post i put out (using Google Groups) that got dropped by google: i am using gcc as so: $ gcc -v Using built-in specs. Target: i386-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man -- infodir=/usr/share/info --enable-shared --enable-threads=posix -- enable-checking=release --with-system-zlib --enable-__cxa_atexit --
8
5667
by: jonpb | last post by:
Hi, Is it possible to define a implicit operator from base to derived types in C#? I have a Point class and would like to derive a Vector class from it and add a couple new vector related functions, like Normalize(). The problem is Vector cannot use any of the Point operator overloads with a compile time error that there is no implicit conversion from Point to Vector. I tried adding an implicit operator to Vector: public static implicit...
0
9563
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
10144
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9997
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9822
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
8821
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
7366
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
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.