Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Address of a function in C or C++
In C programming, every function is stored in the computer's memory and has a unique memory address, just like variables. We can access and display these function addresses to understand how functions are stored in memory.
Syntax
functionName // Returns address of the function (void*)functionName // Cast to void pointer for printing
Accessing Address of a Function
To access the address of a function, we use its name without parentheses. When we write hello() with parentheses, we're calling the function. But when we write just hello, it gives us the memory address where the function is stored.
Example: Displaying Function Addresses
The following program demonstrates how to get and display the memory addresses of user−defined and main functions −
#include <stdio.h>
// defining a simple function
void my_function() {
printf("Hello TutorialsPoint Learner!\n");
}
int main() {
// calling the function
printf("Calling my_function: ");
my_function();
// printing the address of my_function
printf("The address of my_function is: %p\n", (void*)my_function);
// printing the address of main
printf("The address of main is: %p\n", (void*)main);
return 0;
}
Calling my_function: Hello TutorialsPoint Learner! The address of my_function is: 0x559cc9037189 The address of main is: 0x559cc90371bf
Key Points
- Function names without parentheses represent the function's address
- We cast to
(void*)when printing addresses using%pformat specifier - Function addresses are determined at compile/load time and remain constant during execution
- These addresses can be used for function pointers and callback mechanisms
Conclusion
Functions in C have memory addresses that can be accessed using the function name without parentheses. This feature is fundamental for implementing function pointers and callback functions in C programming.
