0
0
Cprogramming~10 mins

Common pointer errors - 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.
Confusing pointer declaration with address-of operator.
2fill in blank
medium

Complete 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'
A*
B&
C%
D->
Attempts:
3 left
💡 Hint
Common Mistakes
Using * instead of & when assigning address to pointer.
Trying to assign the value instead of the address.
3fill in blank
hard

Fix 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'
Ap*
Bp
C&p
D*p
Attempts:
3 left
💡 Hint
Common Mistakes
Using the pointer variable itself instead of dereferencing it.
Using & operator which gives the address, not the value.
4fill in blank
hard

Fill 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'
A*
B&
Cp[0]
D[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using & instead of * to declare pointer.
Trying to dereference pointer incorrectly.
5fill in blank
hard

Fill 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'
Amalloc
B42
Cfree
Dsizeof
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.