← Back to C Course | Chapter 7: Pointers | Lesson 1 of 9

C Memory Address

Every variable lives at a specific memory address, retrieved with the & operator and printed with %p, and understanding addresses is the essential foundation for understanding pointers.
Syntax
c
&variable_name;  // address of the variable

printf("%p", (void*)&variable_name);

What is a Memory Address?

Every piece of data in a running program is stored somewhere in the computer's memory, and that specific location is called its memory address, a number that uniquely identifies where a value physically lives.

Example: What is a Memory Address?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	// Print formatted text to the screen
	printf("%p", (void*)&x);
	// Return 0 to signal that the program finished successfully
	return 0;
}

The Address-of Operator &

The & operator, called the address-of operator, is placed directly before a variable's name to retrieve that variable's memory address instead of its current value.

Example: The Address-of Operator &

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare count and set it to 10
	int count = 10;
	// Print formatted text to the screen
	printf("%p", (void*)&count);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Printing an Address

The %p format specifier is used with printf specifically for displaying memory addresses, and the address is conventionally cast to (void*) before being printed.

Example: Printing an Address

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	// Print formatted text to the screen
	printf("%p", (void*)&x);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Every Variable Has an Address

Every variable in a C program has a memory address, including individual array elements, struct instances, and even function parameters, since each represents a distinct piece of storage.

Example: Every Variable Has an Address

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[3] = {1, 2, 3};
	// Print formatted text to the screen
	printf("%p", (void*)&arr[0]);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Addresses and Pointers

A pointer is a variable specifically designed to store a memory address, and understanding what an address is and how & retrieves one is the essential foundation for understanding how pointers work.

Example: Addresses and Pointers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	int *ptr = &x;
	// Print formatted text to the screen
	printf("%d", *ptr);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Printing an address with %d instead of %p, which is wrong on 64-bit systems.
  2. Confusing &x (address of x) with x (its value).
  3. Assuming an address stays the same on every run, when it can change.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.