ProgrammingHardWipro · Cognizant pattern

Assume a 64-bit LP64 ABI. What is printed?

Assume a 64-bit LP64 ABI. What is printed?
int *findMax(int *arr, int n) {
    int *maxPtr = arr;
    for (int i = 1; i < n; i++)
        if (arr[i] > *maxPtr) maxPtr = &arr[i];
    return maxPtr;
}
int main(void) {
    int nums[5] = {3, 7, 2, 9, 4};
    int *m = findMax(nums, 5);
    printf("%d %ld", *m, m - nums);
}
  1. A. 4 4
  2. B. 9 3
  3. C. 7 1
  4. D. 9 4

Correct answer: B. 9 3

Scanning for the maximum: 3,7,2,9,4 -> 9 is the max, found at index 3 (nums[3]). m points there, so *m=9 and m-nums (pointer difference in elements) = 3.

More C Programming questions

Exams that ask this topic