0
0
C++programming~10 mins

Pointer and variable relationship 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] ptr = &x;
Drag options to blanks, or click blank then click option'
A*
B&
C#
D%
Attempts:
3 left
💡 Hint
Common Mistakes
Using & instead of * when declaring a pointer.
Confusing pointer declaration with address-of operator.
2fill in blank
medium

Complete 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'
A*
B&&
C%
D&
Attempts:
3 left
💡 Hint
Common Mistakes
Using * instead of & when assigning address.
Using && which is for rvalue references, not addresses.
3fill in blank
hard

Fix 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'
A&ptr
Bptr
C*ptr
D&&ptr
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.
4fill in blank
hard

Fill 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'
Aint*
Bint
C&
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using int instead of int* for pointer declaration.
Using *y instead of &y when assigning address.
5fill in blank
hard

Fill 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'
Aint*
B&
C*
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using int instead of int* for pointer declaration.
Assigning *z instead of &z.
Printing ptr instead of *ptr.