Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a pointer to an integer.
C
int [1] ptr; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using & instead of * to declare 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 p.
C
int x = 10; int *p; p = [1]x;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using * instead of & when assigning address to pointer.
Trying to assign the value instead of the address.
✗ Incorrect
The & operator gives the address of a variable, which can be assigned to a pointer.
3fill in blank
hardFix the error in the code to correctly access the value pointed by p.
C
int x = 5; int *p = &x; int y = [1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the pointer variable itself instead of dereferencing it.
Using & operator which gives the address, not the value.
✗ Incorrect
The * operator is used to dereference a pointer and access the value it points to.
4fill in blank
hardFill both blanks to create a pointer to an array and access its first element.
C
int arr[3] = {1, 2, 3}; int [1] p = arr; int first = [2];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using & instead of * to declare pointer.
Trying to dereference pointer incorrectly.
✗ Incorrect
Declaring *p makes p a pointer. Accessing p[0] gets the first element of the array.
5fill in blank
hardFill all three blanks to correctly allocate memory, assign value, and free it.
C
#include <stdlib.h> int *p = (int*)[1](sizeof(int)); *p = [2]; [3](p);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to cast malloc's return value.
Not freeing allocated memory causing memory leaks.
Assigning value to pointer instead of dereferenced pointer.
✗ Incorrect
malloc allocates memory, 42 is assigned to the allocated space, and free releases the memory.