printf("%p\n",(void *) ptr); 
Author Message
 printf("%p\n",(void *) ptr);

Given:

int
main(void)
{
  char *ptr;

  printf("%p\n", (void *) ptr);

Quote:
}

There was this discussion some time back that ptr *had* to be case to
void *.

The rationale being that the prototype for printf is:

int printf(const char *, ...);

thus the compiler cannot do an implicit conversion from
char * -> void *

Now,

%p prints type of void *
%f prints type of double

However, in Harbison & Steele, 3rd edition, page 325, they say
about the `f' conversion that is used in printf.

"If an argument of type float is supplied, it is converted to type double
by the usual argument promotions, so it does work to use %f to print a
number of type float."

Question:

Why is there an implicit conversion from float -> double
but not, char * -> void *

Perhaps, entirely two different mechanisms are in play?
I would appreciate some insight into this.

thanks,
Senthil



Thu, 12 Feb 1998 03:00:00 GMT  
 printf("%p\n",(void *) ptr);


Quote:
>Question:

>Why is there an implicit conversion from float -> double
>but not, char * -> void *

Right from the earliest days of C where prototypes didn't exist there
were a set of standard conversions to function arguments which the standard
now terms the 'default argument promotions'. These are

char types are converted to an int type

short types are converted to an int type

float is converted to double.

There are no default pointer conversions.

ANSI C introduced prototypes which can indicate the target type of a function
argument. The one time they don't do this is in the ... part of a variable
argument this. In this case the default argument promotions apply just as
in the case of a function call where no prototype is in scope. It is therefore
impossible to pass a char, short or float to printf because the compiler
will always convert them to a (possibly unsigned) int or a float.

Quote:
>Perhaps, entirely two different mechanisms are in play?
>I would appreciate some insight into this.

Not really. The format specifier should always match the type of the
argument ultimately passed. In the case of pointers %p requires a void *
type and you must cast the argument since the compiler won't convert it
for you. In the case of a float you know the compiler will convert it to a
double before passing it so you must use a format specifier for a double.

--
-----------------------------------------


-----------------------------------------



Thu, 12 Feb 1998 03:00:00 GMT  
 printf("%p\n",(void *) ptr);


[introductory stuff snipped]

Quote:
>Why is there an implicit conversion from float -> double
>but not, char * -> void *
>Perhaps, entirely two different mechanisms are in play?
>I would appreciate some insight into this.

There are people active in comp.std.c who are better placed to answer
"why".  From the point of view of those who use the language, rather
than define it, the important thing is what promotions the standard
defines for promotion of arguments in non-prototyped functions.  The
only ones defined (6.3.2.2) are the integral promotions (6.2.1.1.) and
float->double.

Since it is a good practice to prototype always, either explicitly or
through the use of appropriate #includes, this can be considered a
non-issue except for variadic functions.  In that case, just know what
is needed and supply it.

--

* Chicago, IL (USA)    



Fri, 13 Feb 1998 03:00:00 GMT  
 printf("%p\n",(void *) ptr);

Quote:
A. Wright) writes:

<snip>
|> >  printf("%p\n", (void *) ptr);
|> >}
|> >
|> >There was this discussion some time back that ptr *had* to be case to
|> >void *.
|> >
|> >The rationale being that the prototype for printf is:
|> >
|> >int printf(const char *, ...);
|> >
|> >thus the compiler cannot do an implicit conversion from
|> >char * -> void *
|> >
|> [snip]
|>
|> Umm....Maybe I'm missing the whole point here, but isn't the char * in the
|> prototype the _format_ string? I.E. "%p\n", which is just text and has
nothing  
|> to do with the types that follow?

Just like "%d" _when passed as a format string to printf_ means that an int
argument follows, "%p\n" _when passed as a format string to printf_ means a
(void*) pointer follows.

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87544-0285,USA H:#3,802,9 St,NM87545
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Tue, 24 Feb 1998 03:00:00 GMT  
 printf("%p\n",(void *) ptr);


Quote:
>Given:

>int
>main(void)
>{
>  char *ptr;

>  printf("%p\n", (void *) ptr);
>}

>There was this discussion some time back that ptr *had* to be case to
>void *.

>The rationale being that the prototype for printf is:

>int printf(const char *, ...);

>thus the compiler cannot do an implicit conversion from
>char * -> void *

[snip]

Umm....Maybe I'm missing the whole point here, but isn't the char * in the
prototype the _format_ string? I.E. "%p\n", which is just text and has nothing
to do with the types that follow?

Ruth -"too lazy to have a witty .sig"



Tue, 24 Feb 1998 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Function ptr equivalent of "NULL"?

2. char *ptr="Is memory allocated here?"

3. What's The Difference Between "typedef void *" and "void *"??

4. problem with "void"

5. "void main" is a lifestyle choice

6. what is "static void usage()"?

7. printf("%d",1.0/3*3)

8. printf("/033E")

9. printf("%g") to 3 sig figs

10. "implicit declaration of function printf"

11. Behaviour of printf("[%.0g]",1.0)

12. "printf()" hangs machine

 

 
Powered by phpBB® Forum Software