What is the value of a function pointer pointer?

Trying to figure out what a function pointer is? Is this the address in the code segment where the function is located?

For example: this piece of code:

#include <stdio.h> void foo(void) { } int main(void) { int a = 10; printf("a address: %p\n", &a); printf("foo address: %p\n", foo); return 0; } 

... prints this:

 [sh/prog-exercises/adam]:./a.out a address: 0xbfffb414 foo address: 0x8048430 

I guess I'm a little confused about how exactly the stack / heap of the process is related to the ELF data segment / code segment. Any helpful pointers would really be welcome. Also, my first question is, so please be careful, I am really trying to learn. Thanks!

+7
c pointers function-pointers
source share
3 answers

That the address of the function entry point is the beginning of its code. The variable a is on the stack, so it is not surprising that its address is significantly different - the stack and code are assigned to different areas of the code. Taht can be quite far apart.

+5
source share

This image is from a UNIX book : system programming should make it clear.

At the bottom of the diagram is the text of the program (low addresses). How can I check from your program. This is where foo () will be.

alt text

+3
source share

Keep in mind that your code is not fully compliant. If you use gcc -Wall -Wextra -pedantic -ansi, you will get a complaint about the line where you print the function pointer. This is because the standard does not guarantee that function pointers can be converted to void pointers. C is designed to work on many different architectures, and if the architecture is Harvard architecture (separate instruction and data memory), there may be good reasons to make these pointers different.

I suppose that in practice this hardly matters, but a good fact to be aware of. Esp when you try to find out the intimate details of C.

+2
source share

All Articles