C ยท Chapter 22 of 45

C Memory Address

Every variable in a running program is stored somewhere in memory, and that location has a numeric address. The & (address-of) operator lets you find out where a variable lives.

Understanding memory addresses is the foundation for pointers, one of C's most powerful (and tricky) features.

Syntax
&variable

The address-of operator

Placing & before a variable name gives you its memory address rather than its value. Addresses are usually printed with the %p format specifier.

Why addresses matter

Functions like scanf need addresses to modify variables outside their own scope, and pointers store addresses so they can indirectly access or modify other variables.

Example 1 (c)
#include <stdio.h>

int main() {
  int age = 25;
  printf("%p\n", (void*)&age);
  return 0;
}
Output
0x7ffee3a1b4ac

&age gives the memory address where age is stored; exact addresses vary each run.

Example 2 (c)
#include <stdio.h>

int main() {
  int a = 5, b = 5;
  printf("%d\n", &a == &b);
  return 0;
}
Output
0

Two different variables have two different addresses, even if their values are the same.

Key points

  • The & operator returns a variable's memory address.
  • Memory addresses are typically printed with %p.
  • Each variable has its own unique address.
  • Memory addresses are the basis for how pointers work.
๐Ÿ’ก Note: Memory addresses printed on your system will differ every time you run a program โ€” that's normal and expected.

๐Ÿ“ Quick Quiz

1. Which operator returns a variable's memory address?

2. Which format specifier is used to print an address?

3. Do two different variables ever share the same address?