S
Sweety
hi,
Is main function address is 657.
its show in all compiler.
try it & say why?
bye,
Is main function address is 657.
its show in all compiler.
try it & say why?
bye,
Sweety said:hi,
Is main function address is 657.
its show in all compiler.
try it & say why?
bye,
Lew said:Sweety wrote:
|
| Is main function address is 657.
Perhaps, perhaps not.
It depends on your compiler, linker, operating system, and CPU.
| its show in all compiler.
No, it doesn't.
~ ~/code $ cat printmain.c
~ #include <stdio.h>
~ #include <stdlib.h>
~ 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.
#include <stdio.h>> cat printmain.c
main() at 0x8048328> gcc -Wall -std=c99 -pedantic -o printmain printmain.c
> ./printmain
<snip>~ printf("main() at %p\n",(void *)&main);
José de Paula said:Lew Pitcher escreveu:
<snip>
Wouldn't simply
printf("main() at %p\n", (void *)main);
do? Notice the pruning of the & operator. For what I know,
the name of a function is a pointer to that function. Am I right?
Lew Pitcher said:#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("main() at %p\n",(void *)&main);
return EXIT_SUCCESS;
}
Martin said:Is the cast to `void *' valid?
Lew Pitcher said:-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Sweety wrote:
| hi,
| Is main function address is 657.
Perhaps, perhaps not. It depends on your compiler, linker, operating system, and
CPU.
| its show in all compiler.
No, it doesn't.
~ ~/code $ cat printmain.c
~ #include <stdio.h>
~ #include <stdlib.h>
~ int main(void)
~ {
~ printf("main() at %p\n",(void *)&main);
~ return EXIT_SUCCESS;
~ }
~ ~/code $ cc -o printmain printmain.c
~ ~/code $ printmain
~ main() at 0x8048328
Lew Pitcher said:José de Paula wrote:
| Em Tue, 06 Apr 2004 21:51:22 -0400, Lew Pitcher escreveu:
| <snip>
|
|>~ printf("main() at %p\n",(void *)&main);
|
| <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?
You are correct. The & was unnecessary.
Martin Dickopp said:Is the cast to `void *' valid?
Lew said:-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
pete wrote:
| Martin Dickopp wrote:
|
|>
|>
|>>#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.
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.
^^^^^^^^^^^^^In said:~ printf("main() at %p\n",(void *)&main);
In said:Is the cast to `void *' valid? I cannot find anything in the standard
which allows a pointer to a function to be converted to type `void *'.
In said: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 said:Lew said:-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
pete wrote:
| Martin Dickopp wrote:
|
|>
|>
|>>#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.
I interpret it as meaning that printing the address of a function
isn't something that you are guaranteed to be able to do.
Richard said: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.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.