Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get the address of variable num.
C++
int num = 10; int* ptr = [1]num;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the dereference operator
* instead of the address-of operator &.Using symbols like
# or % which are not related to pointers.✗ Incorrect
The address-of operator
& is used to get the memory address of a variable.2fill in blank
mediumComplete the code to access the value pointed to by ptr.
C++
int num = 20; int* ptr = # int value = [1]ptr;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the address-of operator
& instead of dereference operator *.Using the arrow operator
-> which is for pointers to structs or classes.✗ Incorrect
The dereference operator
* is used to access the value stored at the memory address a pointer holds.3fill in blank
hardFix the error in the code to correctly assign the value 50 to the variable pointed to by ptr.
C++
int num = 0; int* ptr = # [1]ptr = 50;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning directly to the pointer variable instead of the value it points to.
Using incorrect syntax like
ptr* or &ptr.✗ Incorrect
To assign a value to the variable pointed to by a pointer, use the dereference operator
* before the pointer name.4fill in blank
hardFill both blanks to create a pointer ptr that points to num and then access its value.
C++
int num = 100; int* ptr = [1]num; int val = [2]ptr;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the operators in the blanks.
Using the arrow operator
-> which is not for simple pointers.✗ Incorrect
Use
& to get the address of num and * to access the value pointed to by ptr.5fill in blank
hardFill all three blanks to declare an integer num, a pointer ptr to num, and assign the value 75 to num using the pointer.
C++
int [1]; int* [2] = &[3]; *ptr = 75;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the variable and pointer inconsistently.
Not using the address-of operator
& in the pointer initialization.✗ Incorrect
Declare
num as the integer variable, ptr as a pointer to num, and use &num to get the address.