Daros7
Daros7

Reputation: 63

What type of address is the function pointer address?

Having such a simple C++ function pointer example:

#include <stdio.h>

void my_int_func(int x) {
    printf("%d\n", x);
}

int main() {

    void (*foo)(int);
    foo = &my_int_func;

    foo(78);

    return 0;
}

What is the type of the address pointed by the foo? Is it just a relative address of the my_int_func function from the program starting point (the main function) and hence is it always the same (just because it is relative)?

P.S.
Sorry if the questions are obvious/lame but I'm just a beginner in the topic...

Thx for help!

Upvotes: 1

Views: 162

Answers (1)

eerorika
eerorika

Reputation: 238401

Is it just a relative address of the my_int_func function from the program starting point (the main function) and hence is it always the same (just because it is relative)?

The language doesn't specify such details.

In general, addresses stored in pointers are absolute and not relative.

What they point to varies from architecture to architecture. For some architectures, function pointers point to the first instruction. For others, it points to a function descriptor. And there are architectures where additional information is encoded into the low bits of the pointer.

Upvotes: 3

Related Questions