Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the program if the array begins at 65472 and each integer occupies 2 bytes?

#include <stdio.h>

int main()
{
    int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
    printf("%u, %u\n", a+1, &a+1);
    return 0;
}

 

  • A.65474, 65476
  • B.65480, 65496
  • C.65480, 65488
  • D.65474, 65488

Answer: B

Correct answer: 65480, 65496

a is a 3×4 array of 2-byte ints. a alone decays to a pointer to its first row, so adding 1 skips one row of 4 ints:

\[ 65472 + 4 \times 2 = 65480 \]

&a is a pointer to the whole array, so adding 1 skips all 12 ints:

\[ 65472 + 12 \times 2 = 65496 \]

Same address, different type - and the type is what decides how far +1 moves.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.