0
0
C++programming~10 mins

Pointer arithmetic in C++ - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aptr
Bref
Cp
Dpointer
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is a reserved keyword.
Forgetting the asterisk (*) to declare a pointer.
2fill in blank
medium

Complete 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'
A--
B++
C+=
D-=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '--' which moves the pointer backwards.
Using '+=' without a value.
3fill in blank
hard

Fix 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'
A+
B-
C/
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' which moves the pointer backwards.
Using '*' or '/' which are invalid for pointer arithmetic.
4fill in blank
hard

Fill 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'
A+
B-
C--
D++
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' to move forward.
Using '++' instead of '--' to decrement.
5fill in blank
hard

Fill 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'
Ap
B++
Dq
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for the pointer.
Forgetting to increment the pointer before dereferencing.