Menu
Coddy logo textTech

Address-Of Operator

Part of the Logic & Flow section of Coddy's C++ journey — lesson 2 of 56.

Now that you understand what a pointer is, you need to learn how to actually get the memory address of a variable. This is where the address-of operator (&) comes in.

The address-of operator & is placed before a variable name to retrieve its memory address. When you use &variable_name, it returns the location in memory where that variable is stored.

int number = 42;
int* ptr = &number;  // ptr now stores the address of number

In this example, &number gets the memory address of the variable number, and we assign that address to our pointer ptr. The pointer doesn't contain the value 42 - it contains the address where 42 is stored.

This connection between a variable and its pointer is essential for working with memory directly. Once you have a pointer storing an address, you can use it to access or modify the original variable indirectly.

challenge icon

Challenge

Easy
Create a program that demonstrates the address-of operator by working with an integer variable and a pointer. Declare an integer variable named score and initialize it with the value 85. Create a pointer named scorePtr that stores the memory address of the score variable using the address-of operator. Print the memory address stored in the pointer using the following format:
Address: 0x...
For example, if the address is 0x7fff5fbff6ac, your output should be:
Address: 0x7fff5fbff6ac
Note: Do not include square brackets in your output. The 0x prefix is part of the standard hexadecimal representation of a memory address in C++.

Try it yourself

#include <iostream>
using namespace std;

int main() {
    // TODO: Write your code below
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow