Re: get the address of a function??
- From: Simon Biber <news@xxxxxxxxx>
- Date: Fri, 03 Nov 2006 21:36:52 +1100
CBFalconer wrote:
Keith Thompson wrote:That's not what the code is trying to do. p is a function pointer,
but &p is an object pointer (it points to the function pointer). The
code, if I recall correctly, decomposes the representation of the
function pointer to a sequence of bytes to be printed. The results
are implementation-defined, but it's legal, and it's the only portable
way to print (a representation of) the value of a function pointer.
Amazingly enough, it works here, if you will put up with some digit
jumbling, and lack of knowledge of the size involved.
The digit jumbling is due to little-endian representation. You can reverse the bytes if the pointer value makes more sense to you that way.
There is no lack of knowledge of the size involved. You can get it from sizeof. Either sizeof f, or sizeof (int(*)(void)).
[1] c:\c\junk>cat junk.c
#include <stdio.h>
int return10(void) {return 10;}
int main(int argc, char ** argv)
{
int (*f)(void) = return10;
unsigned char *p = (unsigned char *)&f;
int i, n;
i should be size_t
char hex[] = "0123456789abcdef";
for (i = 0; i < 4; i++) {
That should be
for (i = 0; i < sizeof f; i++) {
n = *(p + i);
putchar(hex[(n & 0xf0) >> 4]);
putchar(hex[(n & 0x0f)]);
}
putchar('\n');
return 0;
} /* main */
--
Simon.
.
- Follow-Ups:
- Re: get the address of a function??
- From: CBFalconer
- Re: get the address of a function??
- References:
- get the address of a function??
- From: John
- Re: get the address of a function??
- From: Andrew Poelstra
- Re: get the address of a function??
- From: Simon Biber
- Re: get the address of a function??
- From: pete
- Re: get the address of a function??
- From: Keith Thompson
- Re: get the address of a function??
- From: CBFalconer
- get the address of a function??
- Prev by Date: Re: string search?
- Next by Date: Re: Cannot return values of char variable
- Previous by thread: Re: get the address of a function??
- Next by thread: Re: get the address of a function??
- Index(es):
Relevant Pages
|
|