Re: get the address of a function??



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.
.



Relevant Pages

  • Re: returning function pointer
    ... pete wrote: ... int *func2; ... sizeof a / sizeof *a); ... I made j into a function pointer. ...
    (comp.lang.c)
  • Re: returning function pointer
    ... pete wrote: ... int *func2; ... sizeof a / sizeof *a); ... I made j into a function pointer. ...
    (comp.lang.c)
  • Re: get the address of a function??
    ... but &p is an object pointer (it points to the function pointer). ... way to print (a representation of) the value of a function pointer. ... I didn't care. ... Either sizeof f, or sizeof ). ...
    (comp.lang.c)
  • Re: C objects
    ... A function pointer doesn't necessarily contain the address of the ... representation as a null object pointer. ... pointers represented as all-bits-zero, null function pointers ...
    (comp.lang.c)
  • Re: Casting pointers to functions of different types
    ... fixed type to allow api for storing and calling that function, ... All function pointer types are guaranteed to have the same ... They aren't specifically required to have the same representation. ...
    (comp.lang.c)