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

Discussion

In C, which of the following is TRUE about arrays and pointers ?

  • A.sizeof(array) equals sizeof(pointer)
  • B.Array name can be incremented
  • C.array[i] is equivalent to *(array + i)
  • D.None of these

Answer: C

The true statement is that array[i] is equivalent to *(array + i).

C defines subscripting in terms of pointer arithmetic: in an expression the array name becomes a pointer to its first element, adding i moves the pointer i elements along, and the dereference reads that element. The two forms are interchangeable by the language definition (which is also why the odd form i[array] compiles).

sizeof(array) gives the size of the whole array, for example 40 for ten 4-byte ints, not the size of a pointer; they coincide only for an array parameter, which is really a pointer. An array name is not a modifiable lvalue, so array++ is an error. Since C is true, 'None of these' is wrong.

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

Post your comment

Please log in or register to post a comment.