0
0
C++programming~10 mins

Common pointer mistakes 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] ptr;
Drag options to blanks, or click blank then click option'
A*
B#
C&&
D&
Attempts:
3 left
💡 Hint
Common Mistakes
Using & instead of * to declare a pointer.
2fill in blank
medium

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

C++
int x = 10;
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 *x instead of &x when assigning to a pointer.
3fill in blank
hard

Fix the error in dereferencing the pointer to get the value of x.

C++
int x = 5;
int* ptr = &x;
int value = [1]ptr;
Drag options to blanks, or click blank then click option'
A&
Bptr
C->
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using &ptr instead of *ptr to get the value.
4fill in blank
hard

Fill both blanks to correctly allocate and assign memory for an integer pointer.

C++
int* ptr = [1] int;
*ptr = [2];
Drag options to blanks, or click blank then click option'
Anew
B5
Cmalloc
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using malloc in C++ without casting or forgetting to assign value.
5fill in blank
hard

Fill all three blanks to safely delete a dynamically allocated pointer and avoid dangling pointer.

C++
int* ptr = new int(20);
delete [1];
[2] = [3];
Drag options to blanks, or click blank then click option'
Aptr
Cnullptr
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to delete allocated memory or not setting pointer to nullptr after deletion.