A2oz

How Do You Print a Pointer Address?

Published in Programming 1 min read

You can print the address of a pointer using the address-of operator (&) and the printf() function.

Here's how:

  1. Declare a pointer variable:

    int *ptr;
  2. Assign a memory address to the pointer:

    int num = 10;
    ptr = # 
  3. 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 = &num;

  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 in printf() 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).

Related Articles