ЁЯУв Too many exams? DonтАЩt know which one suits you best? Book Your Free Expert ЁЯСЙ call Now!


    Question

    Consider the following code snippet. What is the output

    of the program?┬а #include < stdio.h > int main ( ) { ┬а ┬а int arr[5] = {1, 2, 3, 4, 5}; ┬а ┬а int *ptr = arr; ┬а ┬а ptr + = 2; ┬а ┬а printf("%d", *ptr); ┬а ┬а return 0; }
    A 1 Correct Answer Incorrect Answer
    B 2 Correct Answer Incorrect Answer
    C 3 Correct Answer Incorrect Answer
    D 4 Correct Answer Incorrect Answer
    E 5 Correct Answer Incorrect Answer

    Solution

    In the code, ptr initially points to the first element of the array arr. The line ptr += 2; moves the pointer two positions ahead, meaning ptr now points to the third element of the array, which is 3. Therefore, *ptr dereferences the pointer to print the value 3. Why Other Options are Wrong: a) 1 is the first element, but the pointer has moved past it. b) 2 is the second element, but ptr skips over it. d) 4 is the fourth element, but ptr stops at the third. e) 5 is the fifth element, beyond where ptr is pointing.

    Practice Next
    More Data Analytics Languages Questions
    ask-question