Computer >> Computer tutorials >  >> Programming >> C programming

What is the Address of a function in C or C++?


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. Every block of code in the program has its own memory location in the program. Which means like any variable or object methods and functions also have memory address.

To get the memory address of a function you need to use the pointer of the method and write the name of the function without parentheses.

Example

#include<iostream>
using namespace std;
void myfunc(){
   cout<<"This is myfunc";
}
int main(void){
   printf("address of function main() is :%p\n", main);
   printf("address of function myfunc() is : %p\n", myfunc);
   return 0;
}

Output

address of function main() is :0x40079d
address of function myfunc() is : 0x400787