Re: main function address

From: Lew Pitcher (Lew.Pitcher_at_td.com)
Date: 04/07/04


Date: Wed, 07 Apr 2004 07:47:04 -0400


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

pete wrote:
| Martin Dickopp wrote:
|
|>Lew Pitcher <lpitcher@sympatico.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)

iD8DBQFAc+o1agVFX4UWr64RAgMKAJ9jpwGgLD0Ib6+SrFveX/B2DiSkQQCgn1b9
hcqV26vMzT9PW6/myM6CbsQ=
=jmLW
-----END PGP SIGNATURE-----