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] ptr = &x;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using & instead of * when declaring a pointer.
Confusing pointer declaration with address-of operator.
✗ 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 = 5; int* ptr; ptr = [1]x;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using * instead of & when assigning address.
Using && which is for rvalue references, not addresses.
✗ Incorrect
The ampersand (&) operator gives the address of a variable.
3fill in blank
hardFix the error in the code to correctly print the value pointed to by ptr.
C++
int x = 20; int* ptr = &x; std::cout << [1]ptr << std::endl;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Printing the pointer itself instead of the value it points to.
Using &ptr which gives the address of the pointer variable, not the value.
✗ Incorrect
Using *ptr dereferences the pointer to get the value it points to.
4fill in blank
hardFill both blanks to declare a pointer and assign it the address of variable y.
C++
int y = 15; [1] ptr = [2]y;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using int instead of int* for pointer declaration.
Using *y instead of &y when assigning address.
✗ Incorrect
int* declares a pointer to int, and &y gets the address of y.
5fill in blank
hardFill all three blanks to declare a pointer, assign it the address of z, and print the value pointed to by ptr.
C++
int z = 30; [1] ptr = [2]z; std::cout << [3]ptr << std::endl;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using int instead of int* for pointer declaration.
Assigning *z instead of &z.
Printing ptr instead of *ptr.
✗ Incorrect
int* declares the pointer, &z gets the address, and *ptr dereferences to the value.