📖 Scenario: Imagine you have a box with a label showing its address. You want to learn how to use a pointer to find out where the box is and what is inside it.
🎯 Goal: You will create a variable and a pointer that points to it. Then you will print the variable's value, the pointer's value (the address), and the value the pointer points to.
📋 What You'll Learn
Create an integer variable named number with the value 42.
Create an integer pointer named ptr that points to number.
Print the value of number.
Print the value of ptr (the address of number).
Print the value pointed to by ptr.
💡 Why This Matters
🌍 Real World
Pointers are used in programming to directly access and modify memory, which is important for efficient programs and working with hardware.
💼 Career
Understanding pointers is essential for jobs in systems programming, embedded systems, game development, and performance-critical applications.
Progress0 / 4 steps
1
Create an integer variable
Create an integer variable called number and set it to 42.
C++
Hint
Use int number = 42; to create the variable.
2
Create a pointer to the variable
Create an integer pointer called ptr that points to the variable number.
C++
Hint
Use int* ptr = &number; to create the pointer.
3
Print the variable and pointer values
Use std::cout to print the value of number, the value of ptr (the address), and the value pointed to by ptr. Use separate std::cout lines for each.
C++
Hint
Use std::cout << number << std::endl; to print the variable value, and similarly for the pointer and the value it points to.
4
Run and observe the output
Run the program and observe the output. It should print the value 42 twice and the memory address once (the address will look like a hexadecimal number).
C++
Hint
The first and last lines printed should be 42. The middle line is the address and will vary.
Practice
(1/5)
1. What does a pointer variable in C++ store?
easy
A. The value of another variable
B. The memory address of another variable
C. The size of a variable
D. The type of a variable
Solution
Step 1: Understand what a pointer is
A pointer is a variable that stores the address of another variable, not the value itself.
Step 2: Differentiate pointer from normal variables
Normal variables store values directly, but pointers store where those values are located in memory.
Final Answer:
The memory address of another variable -> Option B
Quick Check:
Pointer = address storage [OK]
Hint: Pointers always hold addresses, not values [OK]
Common Mistakes:
Confusing pointer value with variable value
Thinking pointer stores variable type
Assuming pointer stores variable size
2. Which of the following is the correct way to declare a pointer to an integer in C++?
easy
A. int ptr*;
B. *int ptr;
C. int *ptr;
D. ptr int*;
Solution
Step 1: Recall pointer declaration syntax
In C++, to declare a pointer to an int, you write the type followed by an asterisk and the pointer name.
Step 2: Check each option
Only 'int *ptr;' matches the correct syntax. Others have misplaced asterisks or wrong order.
Final Answer:
int *ptr; -> Option C
Quick Check:
Pointer declaration = type *name [OK]
Hint: Pointer declaration: type *name; [OK]
Common Mistakes:
Placing * after variable name
Using * before type keyword
Mixing order of type and pointer symbol
3. What is the output of the following code?
int x = 10;
int *p = &x;
*p = 20;
std::cout << x << std::endl;
medium
A. 20
B. 10
C. Address of x
D. Compilation error
Solution
Step 1: Understand pointer assignment
Pointer p stores the address of x. Using *p = 20 changes the value at that address, so x becomes 20.
Step 2: Output the value of x
After changing *p, printing x outputs 20.
Final Answer:
20 -> Option A
Quick Check:
Pointer dereference changes variable value [OK]
Hint: Changing *p changes x's value [OK]
Common Mistakes:
Thinking *p = 20 changes pointer, not value
Expecting output to be address
Confusing pointer and variable values
4. Identify the error in the following code:
int x = 5;
int *p;
*p = x;
std::cout << *p << std::endl;
medium
A. Pointer p is not initialized before dereferencing
B. Variable x is not declared
C. Incorrect pointer declaration syntax
D. Missing semicolon after declaration
Solution
Step 1: Check pointer initialization
Pointer p is declared but not assigned an address before using *p = x; which causes undefined behavior.