2

An array reference (that is, any mention of an array in a value context), turns into a pointer.

C-FAQ

Is there a program (that compiles) where if you substitute an array value by a pointer value you get different behavior?

Let "different behavior" mean a different computed result, not how any compiler will compile the code differently to give the same result or how one will compile while the other not.

thwd
  • 23,956
  • 8
  • 74
  • 108
  • An example: the `sizeof` operator behaves differently. – juanchopanza Oct 14 '14 at 15:16
  • Another example: you can't apply `++` to an array, but you can to a pointer of course. – Paul R Oct 14 '14 at 15:18
  • try playing with a `2d int array` and a `pointer to pointer to int`. The 2 behave in a very different way. Co-coincidently me and my colleague were discussing this very issue from 2 days, and have settled on a satisfying result.. – Haris Oct 14 '14 at 15:20

2 Answers2

2

Array reference turns to pointer except when it is an operand of sizeof and unary & operator.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • So you're saying array does not turn into a pointer in `a++` or `a =` either – M.M Oct 15 '14 at 12:02
  • @MattMcNabb; I didn't said about that. I said *array names are non-modifiable l-values* unlike pointers. – haccks Oct 15 '14 at 12:08
  • You were unclear about whether or not it turns into a pointer in those cases – M.M Oct 15 '14 at 12:12
  • @MattMcNabb; Removed that part. I am not sure about that. – haccks Oct 15 '14 at 12:13
  • @haccks: Just in case you're interested: They decay for both `++` and `=`. Lvalue conversion results in a non-lvalue, so the constraints for these operators (requiring lvalues as their (left) operands) are violated. – mafso Oct 29 '14 at 21:29
  • @mafso; Thanks for the information. It would be appreciated if you can quote from standard in favour of what you said :) – haccks Oct 31 '14 at 17:52
  • 1
    Sigh. C11 (n1570) 6.3.2.1 p3, 6.5.2.4 p1, 6.5.3.1 p3, 6.5.16 p2. – mafso Oct 31 '14 at 19:28
1

A pointer might not point to a valid memory location (e.g. it may be a NULL pointer). This cannot happen for an array.

bns
  • 113
  • 8
  • and this is a reason for liking programming languages (mostly). You meant _might not_ while I read it first as _can not_. The wonders of ambiguous languages eh? – Baldrickk Oct 14 '14 at 16:11
  • You are right - I'm not a native english speaker. I corrected it. – bns Oct 15 '14 at 11:35
  • I was being humorous. Your original text was correct but ambiguous, as English often is. Your correction is spot on and unambiguous. – Baldrickk Oct 15 '14 at 12:27