Skip to content

Accessing the Address of a Variable in C

In C programming, you can access the address of a variable using the “address-of” operator (&). This operator returns the memory address where the variable is stored in the computer’s memory.

Here’s how you can access the address of a variable:

#include <stdio.h>

int main() {

    int x = 10; // Define a variable ‘x’ and assign it a value

    // Access the address of ‘x’ using the “address-of” operator

    printf(“Address of x: %p\n”, &x);

    return 0;

}

In this example:

  • We define an integer variable x and assign it a value of 10.
  • We use the “address-of” operator (&) to access the memory address of the variable x.
  • The %p format specifier in the printf function is used to print the address in hexadecimal format.

When you run this program, it will print the memory address of the variable x.

Keep in mind:

  • The address returned by the & operator is a hexadecimal value representing the memory location where the variable is stored.
  • Every variable in C has a unique memory address.
  • Accessing the address of a variable is useful when you need to pass the variable’s address to functions, work with pointers, or perform memory-related operations.