0
0
C++programming~10 mins

Pointers and arrays 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.

C++
int* [1];
Drag options to blanks, or click blank then click option'
Aptr
Bpointer
Cp
DintPtr
Attempts:
3 left
💡 Hint
Common Mistakes
Using a type name instead of a variable name.
Forgetting the asterisk (*) to declare a pointer.
2fill in blank
medium

Complete the code to assign the address of variable num to pointer ptr.

C++
ptr = [1]num;
Drag options to blanks, or click blank then click option'
A#
B*
C&&
D&
Attempts:
3 left
💡 Hint
Common Mistakes
Using the dereference operator * instead of the address-of operator.
Using double ampersand && which is incorrect here.
3fill in blank
hard

Fix the error in the code to access the first element of the array using a pointer.

C++
int arr[3] = {1, 2, 3};
int* ptr = arr;
int first = [1]ptr;
Drag options to blanks, or click blank then click option'
Aptr
B&
C*
D->
Attempts:
3 left
💡 Hint
Common Mistakes
Using the address-of operator & instead of dereference.
Using the arrow operator -> which is for pointers to structs or classes.
4fill in blank
hard

Fill both blanks to create a pointer that points to the third element of the array.

C++
int arr[5] = {10, 20, 30, 40, 50};
int* ptr = [1] + [2];
Drag options to blanks, or click blank then click option'
Aarr
B2
C3
D&arr
Attempts:
3 left
💡 Hint
Common Mistakes
Using &arr which is a pointer to the whole array, not the first element.
Adding 3 instead of 2 to point to the third element (index 2).
5fill in blank
hard

Fill all three blanks to create a pointer to an array element and access its value.

C++
int nums[4] = {5, 10, 15, 20};
int* ptr = [1] + [2];
int val = *[3];
Drag options to blanks, or click blank then click option'
Anums
B1
Cptr
Dnums + 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using nums + 2 for the first blank which is incorrect here.
Dereferencing nums instead of the pointer ptr.