C Memory Address
Every variable you create lives somewhere in your computer's memory (RAM). Memory is a long row of bytes, and each byte has a number — its address. When you declare int score = 90;, the compiler reserves four consecutive bytes for score, and the address of the first byte is the address of the variable.
Understanding addresses is the key to pointers, one of the most powerful (and most feared) features of C.
The address-of operator &
Put & in front of a variable to get its address. Addresses are printed with the %p specifier, and should be cast to void * for printf:
#include <stdio.h>
int main(void) {
int score = 90;
printf("value: %d\n", score);
printf("address: %p\n", (void *) &score);
return 0;
}value: 90
address: 0x7ffd5e8a3c4cThe exact address is different on every computer and often on every run — operating systems deliberately place programs at random locations for security (address space layout randomisation). That is why this example is not a runnable playground: its output could never match a saved copy. Try it on your own machine and run it twice.
You have already used & without knowing it: scanf("%d", &score) passes the address of score so that scanf can store a value there.
Addresses of neighbouring elements
Absolute addresses vary, but the distance between them is stable. Array elements are stored right next to each other, so the address of each element is exactly sizeof(element) bytes after the previous one. Subtracting addresses (converted to char * so the difference is measured in bytes) shows this:
#include <stdio.h>
int main(void) {
int nums[4] = {10, 20, 30, 40};
char *base = (char *) &nums[0];
for (int i = 0; i < 4; i++) {
char *addr = (char *) &nums[i];
printf("nums[%d] = %d is %td bytes after nums[0]\n", i, nums[i], addr - base);
}
return 0;
}nums[0] = 10 is 0 bytes after nums[0]
nums[1] = 20 is 4 bytes after nums[0]
nums[2] = 30 is 8 bytes after nums[0]
nums[3] = 40 is 12 bytes after nums[0]
Each int takes 4 bytes, so the elements are 4 bytes apart. With a double array they would be 8 bytes apart.
Comparing addresses
Two different variables always have different addresses, and a variable keeps the same address for its whole lifetime. You can compare addresses with == to check whether two things are the same object in memory:
#include <stdio.h>
int main(void) {
int a = 5;
int b = 5;
printf("a == b (values): %d\n", a == b);
printf("&a == &b (places): %d\n", &a == &b);
printf("&a == &a: %d\n", &a == &a);
int arr[3];
printf("arr == &arr[0]: %d\n", (void *) arr == (void *) &arr[0]);
return 0;
}a == b (values): 1
&a == &b (places): 0
&a == &a: 1
arr == &arr[0]: 1
The last line reveals something important: the name of an array, used in an expression, is the address of its first element. That is why you do not write & when passing a string to scanf("%s", name).
How big is an address?
An address is itself a value that has to be stored somewhere. On a 64-bit system it takes 8 bytes, no matter what type it points to:
#include <stdio.h>
int main(void) {
char c = 'x';
double d = 1.5;
int arr[100];
printf("sizeof(&c) = %zu\n", sizeof(&c));
printf("sizeof(&d) = %zu\n", sizeof(&d));
printf("sizeof(&arr) = %zu\n", sizeof(&arr));
printf("sizeof(arr) = %zu (the array itself)\n", sizeof(arr));
(void) c; (void) d;
return 0;
}sizeof(&c) = 8
sizeof(&d) = 8
sizeof(&arr) = 8
sizeof(arr) = 400 (the array itself)
Why addresses matter
| Situation | Why you need the address |
|---|---|
scanf |
it must write the value into your variable |
| Functions that change their arguments | C passes copies; with the address a function can modify the original |
| Arrays and strings | they are passed to functions as the address of their first element |
| Dynamic memory | malloc gives you an address of fresh memory |
| Data structures | linked lists and trees connect nodes by storing addresses |
A variable that stores an address is called a pointer — the topic of the next lesson.
Tip:
%poutput is written in hexadecimal and its exact format depends on the platform. Use it for debugging, never for program logic.
Test yourself
What does &x give you?
- The value of x
- The address of x in memory
- x multiplied by 2
- A copy of x
Which format specifier prints an address?
- %d
- %a
- %p
- %x
Why does printing &x give different results on different runs?
- The compiler is broken
- The operating system randomises where programs are placed in memory
- x changes its value
- %p prints random numbers