← 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.

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 <stdio.h>
int main() {
	int x = 5;
	printf("%p", (void*)&x);
	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 <stdio.h>
int main() {
	int count = 10;
	printf("%p", (void*)&count);
	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 <stdio.h>
int main() {
	int x = 5;
	printf("%p", (void*)&x);
	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 <stdio.h>
int main() {
	int arr[3] = {1, 2, 3};
	printf("%p", (void*)&arr[0]);
	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 <stdio.h>
int main() {
	int x = 5;
	int *ptr = &x;
	printf("%d", *ptr);
	return 0;
}

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.