Sweety <sweety_elegant @yahoo.co.in> scribbled the following:[color=blue]
> hi,
> Is main function address is 657.
> its show in all compiler.
> try it & say why?
> bye,[/color]
No, the main function's address may be whatever your implementation
wants it to be. You can't rely on it being any particular numeric value,
actually you can't rely it on being a numeric value at all.
--
/-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Keep shooting, sooner or later you're bound to hit something."
- Misfire
Dan.Pop@cern.ch (Dan Pop) wrote:[color=blue]
>
> The easy way out for printf would be something like %q which expects a
> pointer to a void function taking no arguments. However, given that %p
> itself is virtually never used in real world programs, and that
> %lx or %llx do work on the pointer converted to the appropriate integer
> type, there is little point on adding %q.[/color]
%p is indispensable[*] on systems without flat address space
(for example, on MS-DOS it prints segment:offset, on 8051 it tells you
which page the pointer points to, and presumably on these "semi-64 bit"
contraptions that are coming out, it will print segment:offset) .
Admittedly I've never used it except in recording of debug information.
Windows 3.1 crash messages printed their addresses in the same format,
so I would conjecture that Windows still uses "%p" to print the
address of where programs crashed (virtually every day, in real world PCs)
[*] Of course you can do the (unsigned char *)&ptr thing but that is tedious,
and using %lx is relying on undefined behaviour.
~ int main(void)
~ {
~ printf("main() at %p\n",(void *)&main);
~ return EXIT_SUCCESS;
~ }
~ ~/code $ cc -o printmain printmain.c
~ ~/code $ printmain
~ main() at 0x8048328
~ ~/code $
| try it & say why?
I did, and it' didn't, so I can't say why 657.
| bye,
- --
Lew Pitcher
Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
Em Tue, 06 Apr 2004 21:51:22 -0400, Lew Pitcher escreveu:
<snip>[color=blue]
> ~ printf("main() at %p\n",(void *)&main);[/color]
<snip>
Wouldn't simply
printf("main() at %p\n", (void *)main);
do? Notice the prunning of the & operator. For what I know,
the name of a function is a pointer to that function, am I right?
Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
In <843a4f78.04041 41850.166ab63b@ posting.google. com> oldwolf@inspire .net.nz (Old Wolf) writes:
[color=blue]
>Dan.Pop@cern.c h (Dan Pop) wrote:[color=green]
>>
>> The easy way out for printf would be something like %q which expects a
>> pointer to a void function taking no arguments. However, given that %p
>> itself is virtually never used in real world programs, and that
>> %lx or %llx do work on the pointer converted to the appropriate integer
>> type, there is little point on adding %q.[/color]
>
>%p is indispensable[*] on systems without flat address space
>(for example, on MS-DOS it prints segment:offset, on 8051 it tells you
>which page the pointer points to, and presumably on these "semi-64 bit"
>contraptions that are coming out, it will print segment:offset) .
>Admittedly I've never used it except in recording of debug information.
>
>Windows 3.1 crash messages printed their addresses in the same format,
>so I would conjecture that Windows still uses "%p" to print the
>address of where programs crashed (virtually every day, in real world PCs)[/color]
Have I ever advocating dropping %p from the standard? If not, what
exactly is your point?
[color=blue]
>[*] Of course you can do the (unsigned char *)&ptr thing but that is tedious,
>and using %lx is relying on undefined behaviour.[/color]
You got it wrong: using %p for function pointers *is* relying on undefined
behaviour, using %lx for function pointers converted to unsigned long is
NOT undefined behaviour (such conversions are *explicitly* allowed by the
standard).
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
Martin Dickopp wrote:[color=blue]
>
> Lew Pitcher <lpitcher@sympa tico.ca> writes:
>[color=green]
> > #include <stdio.h>
> > #include <stdlib.h>
> >
> > int main(void)
> > {
> > printf("main() at %p\n",(void *)&main);
> >
> > return EXIT_SUCCESS;
> > }[/color]
>
> Is the cast to `void *' valid?[/color]
No.
In N869, it's one of the common extensions.
J.5.7 Function pointer casts
[#2] A pointer to a function may be cast to a pointer to an
object or to void, allowing a function to be inspected or
modified (for example, by a debugger) (6.5.4).
.... which makes it more obviously not part of standard C.
It's unnecessary, but harmless, and some might argue that it's
clearer, since it makes it more explicit that the expression is an
address.
A function name is implicitly converted to a function pointer unless
it's the operand of a sizeof or unary & operator. (In the case of
sizeof, the resulting expression is illegal.)
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Martin Dickopp <expires-2004-05-31@zero-based.org> wrote:
[color=blue]
> Lew Pitcher <lpitcher@sympa tico.ca> writes:
>[color=green]
> > #include <stdio.h>
> > #include <stdlib.h>
> >
> > int main(void)
> > {
> > printf("main() at %p\n",(void *)&main);
> >
> > return EXIT_SUCCESS;
> > }[/color]
>
> Is the cast to `void *' valid?[/color]
No. Mind you, there is no better way to print the address of a function,
either. Where this works, it works; where it doesn't, nothing else is
likely to.
pete wrote:
| Martin Dickopp wrote:
|
|>Lew Pitcher <lpitcher@sympa tico.ca> writes:
|>
|>
|>>#include <stdio.h>
|>>#include <stdlib.h>
|>>
|>>int main(void)
|>>{
|>> printf("main() at %p\n",(void *)&main);
|>>
|>> return EXIT_SUCCESS;
|>>}
|>
|>Is the cast to `void *' valid?
|
|
| No.
|
| In N869, it's one of the common extensions.
|
| J.5.7 Function pointer casts
| [#2] A pointer to a function may be cast to a pointer to an
| object or to void, allowing a function to be inspected or
| modified (for example, by a debugger) (6.5.4).
|
| ... which makes it more obviously not part of standard C.
In 9989-1999 (admittedly, just the draft C99 standard, and not the
/actual standard itself), the printf() function documentation in
7.19.6.3 refers the reader to the fprintf() documentation for a
description of it's input. The fprintf() documentation in 7.19.6.1 says
of the %p format
~ p The argument shall be a pointer to void. The value of the pointer is
~ converted to a sequence of printing characters, in an
~ implementation-defined manner.
So, to satisfy the %p format character, the argument to
fprintf()/printf() /must/ be a "pointer to void". Since main is a
"pointer to function returning int", and not a "pointer to void", I
interpreted the documentation as requiring a cast to void pointer.
- --
Lew Pitcher, IT Consultant, Enterprise Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group
(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)
Comment