Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a pointer to an integer.
C++
int [1] ptr; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using & instead of * to declare a pointer.
✗ Incorrect
The asterisk (*) is used to declare a pointer variable in C++.
2fill in blank
mediumComplete the code to assign the address of variable x to pointer ptr.
C++
int x = 10; int* ptr; ptr = [1]x;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using *x instead of &x when assigning to a pointer.
✗ Incorrect
The ampersand (&) operator gives the address of a variable.
3fill in blank
hardFix the error in dereferencing the pointer to get the value of x.
C++
int x = 5; int* ptr = &x; int value = [1]ptr;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using &ptr instead of *ptr to get the value.
✗ Incorrect
The * operator dereferences the pointer to access the value it points to.
4fill in blank
hardFill both blanks to correctly allocate and assign memory for an integer pointer.
C++
int* ptr = [1] int; *ptr = [2];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using malloc in C++ without casting or forgetting to assign value.
✗ Incorrect
Use new int to allocate memory and then assign a value to the dereferenced pointer.
5fill in blank
hardFill all three blanks to safely delete a dynamically allocated pointer and avoid dangling pointer.
C++
int* ptr = new int(20); delete [1]; [2] = [3];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to delete allocated memory or not setting pointer to nullptr after deletion.
✗ Incorrect
Use delete ptr; to free memory and then set ptr to nullptr to avoid dangling pointer.