You can print the address of a pointer using the address-of operator (&
) and the printf()
function.
Here's how:
-
Declare a pointer variable:
int *ptr;
-
Assign a memory address to the pointer:
int num = 10; ptr = #
-
Use
printf()
to print the pointer address:printf("Address of num: %p\n", ptr);
Example:
#include <stdio.h>
int main() {
int num = 10;
int *ptr = #
printf("Address of num: %p\n", ptr);
return 0;
}
Output:
Address of num: 0x7fffd79662cc
The output will display the memory address where the variable num
is stored. The actual address will vary depending on your system and program execution.
Key Points:
- The
%p
format specifier inprintf()
is used to print pointer addresses. - The address-of operator (
&
) returns the memory address of a variable. - Pointer addresses are typically displayed in hexadecimal format (e.g.,
0x7fffd79662cc
).