0
0
Cprogramming~10 mins

Pointers and arrays 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 access the first element of the array using a pointer.

C
int arr[3] = {1, 2, 3};
int *p = arr;
int first = [1];
Drag options to blanks, or click blank then click option'
Aarr[1]
Bp[1]
C&arr[0]
D*p
Attempts:
3 left
💡 Hint
Common Mistakes
Using p[1] which accesses the second element, not the first.
3fill in blank
hard

Fix the error in the pointer arithmetic to access the third element.

C
int arr[4] = {10, 20, 30, 40};
int *p = arr;
int val = *[1];
Drag options to blanks, or click blank then click option'
Ap - 2
Bp + 2
Carr + 3
D&p + 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using p - 2 which moves pointer backwards.
4fill in blank
hard

Fill both blanks to create a pointer to the second element and get its value.

C
int nums[5] = {5, 10, 15, 20, 25};
int *ptr = nums [1] 1;
int value = *[2];
Drag options to blanks, or click blank then click option'
A+
B-
Cptr
Dnums
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' to move forward in the array.
5fill in blank
hard

Fill all three blanks to create a pointer, move it, and access the value correctly.

C
int data[4] = {2, 4, 6, 8};
int *p = [1];
p = p [2] 3;
int val = *[3];
Drag options to blanks, or click blank then click option'
Adata
B+
Cp
D&data
Attempts:
3 left
💡 Hint
Common Mistakes
Using '&data' which is pointer to the whole array, not first element.