0
0
Cprogramming~10 mins

Pointer arithmetic - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to access the next element using pointer arithmetic.

C
int arr[] = {10, 20, 30};
int *ptr = arr;
int value = *(ptr [1] 1);
Drag options to blanks, or click blank then click option'
A/
B-
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' moves the pointer backward, which is incorrect here.
2fill in blank
medium

Complete the code to calculate the number of elements between two pointers.

C
int arr[] = {5, 10, 15, 20};
int *start = &arr[0];
int *end = &arr[3];
int diff = end [1] start;
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' will not give the distance between pointers.
3fill in blank
hard

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

C
int nums[] = {1, 2, 3, 4};
int *p = nums;
int val = *(p[1] 2);
Drag options to blanks, or click blank then click option'
A+
B*
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' or '/' causes syntax errors.
4fill in blank
hard

Fill both blanks to create a pointer that points to the second element and then access it.

C
int data[] = {7, 14, 21};
int *ptr = data [1] 1;
int val = [2]ptr;
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' moves pointer backward incorrectly.
Using '/' or other operators causes errors.
5fill in blank
hard

Fill all three blanks to create a pointer to the last element, subtract 2 to get the first, and dereference it.

C
int vals[] = {3, 6, 9};
int *p = vals [1] 2;
int *q = p [2] 2;
int result = [3]q;
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing '+' and '-' incorrectly.
Using '/' causes errors.