What is the output of the following pseudocode ? int arr[] = {1, 2, 3, 4, 5}…
2026
What is the output of the following pseudocode ?
int arr[] = {1, 2, 3, 4, 5}
int *ptr = arr + 2
print(*ptr + *(ptr + 1))Answer: C. 7 — Array Initialization: An array arr is initialized with 5 elements: arr = 1 arr = 2 arr = 3 arr = 4 arr = 5 Pointer Arithmetic: In pointer arithmetic, arr…
- A.
3
- B.
5
- C.
7
- D.
9
Attempted by 444 students.
Show answer & explanation
Correct answer: C
Array Initialization: An array
arris initialized with 5 elements:arr = 1arr = 2arr = 3arr = 4arr = 5
Pointer Arithmetic:
In pointer arithmetic,
arrrefers to the memory address of the first element (arr).arr + 2moves the pointer forward by 2 elements, pointing toarr.Therefore,
int *ptr = arr + 2;setsptrto point to the element 3.
Dereferencing and Addition:
*ptrdereferences the pointer, which gives the value atarr, which is 3.ptr + 1points to the next consecutive element in memory, which isarr.*(ptr + 1)dereferences that next position, giving the value atarr, which is 4.
Final Print:
print(*ptr + *(ptr + 1))becomesprint(3 + 4), which outputs 7.