M
Michael B Allen
I need a macro to compute an offset of an object relative to a base
address. The problem is the object can sometimes be a function pointer
with a base address of 0 but I don't know in advance that the object is
a function. Can it be done?
#define BREF(b,p) ((size_t)((p) ? (char *)(p) - (char *)(b) : 0))
The following code demonstrates that strict c89 doesn't accept casting
a pointer to a function to a pointer to char which makes my above code
invalid.
typedef int (*fn)(void);
void
test(fn f)
{
char *g = (char *)f;
}
int
main(void)
{
test(main);
return 0;
}
cc -std1 q.c
cc: Info: q.c, line 6: In the initializer for g, "f" of type "pointer
to function () returning void", is being converted to "pointer to char".
Such a cast is not permitted by the standard. (nonstandcast)
char *g = (char *)f;
--------------------------^
address. The problem is the object can sometimes be a function pointer
with a base address of 0 but I don't know in advance that the object is
a function. Can it be done?
#define BREF(b,p) ((size_t)((p) ? (char *)(p) - (char *)(b) : 0))
The following code demonstrates that strict c89 doesn't accept casting
a pointer to a function to a pointer to char which makes my above code
invalid.
typedef int (*fn)(void);
void
test(fn f)
{
char *g = (char *)f;
}
int
main(void)
{
test(main);
return 0;
}
cc -std1 q.c
cc: Info: q.c, line 6: In the initializer for g, "f" of type "pointer
to function () returning void", is being converted to "pointer to char".
Such a cast is not permitted by the standard. (nonstandcast)
char *g = (char *)f;
--------------------------^