The Address-of operator &
What you'll learn: How to find the specific location in your computer's memory where a piece of data is stored.
The Mailbox Analogy
Imagine your computer’s memory (RAM) as a long street lined with millions of identical mailboxes. Each mailbox has two important parts: the content inside it (the mail) and a unique address painted on the outside (like 123 Main St).
In C, when you create a variable like int score = 50;, you are putting the "mail" (the number 50) into one of those mailboxes. Usually, we just care about the value inside. However, to understand pointers, we need to look at the "house number" where that value lives.
Meet the & Operator
The & symbol is called the Address-of operator. When you place it in front of a variable name, you aren't asking for the value stored inside; you are asking for the memory address where that variable is located.
Think of it like a GPS for your data. Instead of saying "give me the score," you are saying "tell me where the score is sitting."
Seeing Addresses in Code
In the example below, we use the %p format specifier. The p stands for "pointer," and it tells C to print a memory address, which usually looks like a strange string of numbers and letters (hexadecimal), such as 0x7ffeed.
#include <stdio.h>
int main() {
int cookies = 12;
// Printing the value normally
printf("Value of cookies: %d\n", cookies);
// Printing the location using the & operator
// The %p prints the memory address
printf("Memory address of cookies: %p\n", (void*)&cookies);
return 0;
}
When you run this, you’ll see the number 12, followed by a complex code like 0x7ffee45b8. That code is the exact "mailbox number" where the integer 12 is currently stored in your RAM.
Why it matters
Every time you run your program, your computer might choose a different empty "mailbox" for your variable. The & operator allows your code to be flexible. Instead of hard-coding a location, your program can look up the address dynamically. This is the first step toward "pointing" to data rather than just carrying it around.
Key takeaway
Use the & operator to find the unique memory address of a variable.