C Memory Address
In this page:
&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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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 &
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Printing an address with
%dinstead of%p, which is wrong on 64-bit systems. - Confusing
&x(address of x) withx(its value). - Assuming an address stays the same on every run, when it can change.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: