The Address-Of Operator (&)
Part of the Logic & Flow section of Coddy's C journey — lesson 3 of 63.
Now that you can declare pointers, you need to learn how to actually store a memory address in them. The address-of operator (&) is the key to getting the memory address of any variable.
When you place the & operator in front of a variable name, it returns the memory address where that variable is stored. Think of it as asking "Where does this variable live in memory?"
int age = 25;
int *ptr;
ptr = &age; // Store the address of 'age' in 'ptr'In this example, &age gives us the memory address of the age variable. We then assign this address to our pointer ptr. Now ptr "points to" the age variable.
The address-of operator is essential because it's the bridge between regular variables and pointers. Without it, you couldn't tell a pointer which variable to point to. Remember that the data types must match - you can only assign the address of an integer to a pointer declared for integers.
Challenge
EasyWrite a C program that demonstrates using the address-of operator to connect variables with pointers. Your program should:
- Declare an integer variable named
numberand initialize it with the value 42 - Declare a character variable named
letterand initialize it with the value 'X' - Declare a pointer to an integer named
num_ptr - Declare a pointer to a character named
char_ptr - Use the address-of operator to assign the address of
numbertonum_ptr - Use the address-of operator to assign the address of
lettertochar_ptr - Print the addresses stored in both pointers using
printfwith the%pformat specifier
Your output should display the pointer addresses in the following format:
Address of number: [address]
Address of letter: [address]The actual memory addresses will vary between program runs, but the format and labels must match exactly as shown above.
Cheat sheet
The address-of operator (&) returns the memory address of a variable:
int age = 25;
int *ptr;
ptr = &age; // Store the address of 'age' in 'ptr'The address-of operator is essential for connecting variables with pointers. Data types must match - you can only assign the address of an integer to a pointer declared for integers.
Use %p format specifier to print pointer addresses with printf.
Try it yourself
#include <stdio.h>
int main() {
// TODO: Write your code here
// 1. Declare and initialize the variables
// 2. Declare the pointers
// 3. Use the address-of operator to assign addresses
// 4. Print the addresses using the format shown
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions