Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function pointers and address of a function

So I figured when making function pointers, you do not need the operator & to get the address of the initial function:

#include <stdio.h>  double foo (double x){     return x*x; }  int main () {      double (*fun1)(double) = &foo;     double (*fun2)(double) =  foo;      printf("%f\n",fun1(10));     printf("%f\n",fun2(10));      printf("fun1 = %p \t &foo = %p\n",fun1, &foo);     printf("fun2 = %p \t  foo = %p\n",fun2,  foo);             int a[10];      printf("  a = %p \n &a = %p  \n",a,&a);      return 0; } 

output:

>./a.out  100.000000 100.000000 fun1 = 0x4004f4      &foo = 0x4004f4 fun2 = 0x4004f4       foo = 0x4004f4   a = 0x7fff26804470   &a = 0x7fff26804470  

Then I realized this is also true for arrays, meaning that if you have int a[10] both a and &a point to the same location. Why is that with arrays and functions? Is the address saved in a memory location that has the same address as the value(address) being saved in it?

like image 854
mmirzadeh Avatar asked Mar 04 '12 05:03

mmirzadeh


People also ask

What is the address of a function?

A function is a block of code that is defined to perform a specific piece of work in a program. It is used to ease the work of the programmer by defining a commonly occurring piece of code so that it can be reused when required. The address is the memory location where the entity is stored.

How do I find the address of a function pointer?

Address of a function in C or C++ We all know that code of every function resides in memory and so every function has an address like all others variables in the program. We can get the address of a function by just writing the function's name without parentheses. Please refer function pointer in C for details.

What do you mean by function pointer?

A function pointer, also called a subroutine pointer or procedure pointer, is a pointer that points to a function. As opposed to referencing a data value, a function pointer points to executable code within memory.


2 Answers

Given int a[10], both a and &a yield the same address, yes, but their types are different.

a is of type int[10]. When it is implicitly converted to a pointer type, the pointer is of type int* and points to the initial element of the array. &a is of type int (*)[10] (that is, a pointer to an array of ten integers). Because there can be no padding in an array, they both yield pointers with the same value, but the pointers have different types.

Functions are similar to arrays, but not entirely the same. Your function foo is of type double(double). Whenever foo is used in an expression and is not the operand of the unary & operator, it is implicitly converted to a pointer to itself, which is of type double(*)(double).

So, for all practical purposes, the name of a function and a pointer to the same function are interchangeable. There are some subtleties, all of which I discuss in an answer to "Why do all these crazy function pointer definitions all work? What is really going on?" (That question was asked about C++, but the rules for nonmember functions in C++ are the same as for functions in C.)

like image 127
James McNellis Avatar answered Sep 29 '22 22:09

James McNellis


No, there's no extra storage dedicated to pointing to the function/array.

With most variables variable_name has a meaning other than getting the address of that variable, so you need to use &variable to get the address.

With a function or array, function_name (by itself, not followed by parentheses) doesn't have any other meaning, so there was no problem with interpreting it as taking the address of the function.

Likewise in reverse: a normal pointer needs to be dereferenced explicitly, but a pointer to a function doesn't (again, because there's no other reasonable interpretation), so given a pointer to a function like:

int (*func)(param_list); 

The following are equivalent to each other -- both call whatever function func points at:

(*func)(params);  func(params); 
like image 23
Jerry Coffin Avatar answered Sep 29 '22 21:09

Jerry Coffin