Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a pointer to an integer variable.
C++
int x = 10; int* [1] = &x;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is a reserved keyword.
Forgetting the asterisk (*) to declare a pointer.
✗ Incorrect
The pointer variable name can be any valid identifier. Here, 'p' is used as the pointer name.
2fill in blank
mediumComplete the code to increment the pointer to point to the next integer in memory.
C++
int arr[3] = {1, 2, 3}; int* p = arr; p [1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '--' which moves the pointer backwards.
Using '+=' without a value.
✗ Incorrect
Using '++' increments the pointer to the next integer in the array.
3fill in blank
hardFix the error in the code to correctly access the second element using pointer arithmetic.
C++
int arr[3] = {10, 20, 30}; int* p = arr; int val = *(p [1] 1);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' which moves the pointer backwards.
Using '*' or '/' which are invalid for pointer arithmetic.
✗ Incorrect
Adding 1 to the pointer moves it to the second element, then dereferencing gets its value.
4fill in blank
hardFill both blanks to create a pointer that points to the third element and then decrement it.
C++
int arr[4] = {5, 10, 15, 20}; int* p = arr [1] 2; p [2];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' to move forward.
Using '++' instead of '--' to decrement.
✗ Incorrect
Adding 2 moves pointer to third element; '--' decrements pointer to second element.
5fill in blank
hardFill all three blanks to create a pointer to the first element, move it forward by one, and then access the value.
C++
int arr[3] = {100, 200, 300}; int* [1] = arr; [1] [2]; int val = *[3];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for the pointer.
Forgetting to increment the pointer before dereferencing.
✗ Incorrect
Pointer 'p' is declared, incremented, then dereferenced to get the second element's value.