
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Address of a function in C or C++
In C or C++, each variable is stored in memory and we can access its memory addresses using the address-of operator (&). Similarly, the functions are also stored in memory and also have their own memory addresses.
Accessing Address of a Function
To access the address we can use the function name without parenthesis. This is because adding parentheses means you're calling the function and without parentheses it will refer to its address.
C Example to Access Address of a Function
Here is the following example code in C, referring to the address of a function without using parentheses:
#include <stdio.h> // here we define a function void my_function() { printf("Hello World\n"); } int main() { // calling the function normally by using parentheses printf("Calling function: "); my_function(); // printing the function address without using parentheses printf("The address of my_function is: %p\n", (void*)my_function); // printing the address of the main() function without using parentheses printf("The address of main is: %p\n", (void*)main); return 0; }
Output
Calling function: Hello World The address of my_function is: 0x401136 The address of main is: 0x401147
C++ Example to Access Address of a Function
Here is the following example code in C++, referring to the address of a function without using parentheses:
#include <iostream> using namespace std; // here defining a simple function void my_function() { cout << "Hello TutorialsPoint Learner!" << endl; } int main() { // here calling the function normally using parentheses cout << "Calling my_function: "; my_function(); // here referring to the function's address without using parentheses cout << "The address of my_function is: " << (void*)my_function << endl; // here referring to the address of main function cout << "The address of main() function is: " << (void*)main << endl; return 0; }
Output
Calling my_function: Hello TutorialsPoint Learner! The address of my_function is: 0x55b31d78d189 The address of main() function is: 0x55b31d78d1bf
Advertisements