A pointer is a variable that stores the memory address of another variable.
Click to reveal answer
beginner
How do you declare a pointer to an integer variable?
You declare it using the syntax: int* ptr; where ptr is the pointer variable.
Click to reveal answer
beginner
What does the * operator do when used with a pointer?
It dereferences the pointer, meaning it accesses the value stored at the memory address the pointer holds.
Click to reveal answer
intermediate
Explain the relationship between a pointer and the variable it points to.
A pointer holds the address of a variable. Changing the value through the pointer changes the original variable's value because they refer to the same memory location.
Click to reveal answer
intermediate
What happens if you change the pointer's value itself?
Changing the pointer's value changes the memory address it points to, so it will point to a different variable or memory location.
Click to reveal answer
What does the following code print?
int x = 10; int* p = &x; std::cout << *p;
AGarbage value
BAddress of x
C10
DCompilation error
✗ Incorrect
The pointer p stores the address of x. Using *p accesses the value of x, which is 10.
Which operator is used to get the address of a variable?
A&
B*
C->
D%
✗ Incorrect
The & operator returns the memory address of a variable.
If int* p; is uninitialized, what happens if you try to dereference it?
AIt prints 0
BIt causes undefined behavior (likely crash)
CIt prints the address stored in p
DIt compiles but does nothing
✗ Incorrect
Dereferencing an uninitialized pointer leads to undefined behavior and can crash the program.
What does the pointer store?
AA memory address
BA value
CA variable name
DA data type
✗ Incorrect
Pointers store memory addresses of variables.
How do you change the value of a variable using a pointer?
AYou cannot change variable value through pointer
BChange the pointer variable itself
CUse the & operator on the pointer
DUse the * operator to assign a new value
✗ Incorrect
Using *pointer = new_value changes the value at the memory address the pointer points to.
Explain in your own words how a pointer relates to the variable it points to.
Think about how a pointer is like a remote control to a TV.
You got /3 concepts.
Describe what happens when you change the pointer's value versus when you change the value it points to.
Consider the pointer as an address label and the variable as the house at that address.
You got /3 concepts.
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.