Hello every one,
I have a confusion regarding arrays and pointers, My question is: "Is array name a pointer (rather a constant pointer)"?? I feel the answer is 'yes' because we can use the de-referencing operator(*) with the array name, Also, because array name is a constant pointer so we cannot assign a different address to it.... but the link http://c-faq.com/aryptr/aryptr2.html clearly says that arrays are not pointers....

9 Years
Discussion Span
Last Post by Narue

>My question is: "Is array name a pointer (rather a constant pointer)"??
No, but the reality is similar. An array name is converted to a pointer to the first element most of the time. The official standard terminology is that an array name is converted to a pointer to the first element when used in a value context as opposed to an object context. A value context is when you're retrieving and subsequently using the value of an object, such as the right hand side of x = y; . An object context is when you're modifying or querying the object itself, not the value it contains. The left hand side of x = y; is one example.

People try to make sense of the array-to-pointer conversion rule by saying that the array is converted to a constant pointer, and that's why you can't assign to it or perform modifying arithmetic such as ++a . What really happens is that operators such as ++ see the operand in an object context, and those operators aren't defined for array types. The conversion is not made at all, so pretending that you're working with a constant pointer rather than an array is a fundamental misunderstanding.

There are a few places where an array can legally be used in an object context, which is why saying an array name is a pointer is wrong. Two of the common places an array is used in object context are:

  1. As an operand to the address-of operator, converting to a pointer won't work because then the type would be all muxed up even though the actual address would likely be the same. When you say &a (where a is an array type), you want the result to be a pointer to an array, not a pointer to a pointer. If the conversion happened, this wouldn't work as expected.
  2. As an operand to the sizeof operator, converting to a pointer would produce entirely the wrong result. You'd get the size of a pointer, not the size of the array, which is nearly always not what you expected. A common mistake is using sizeof on an "array" function parameter:
    void foo ( int a[] )
      printf ( "%zd\n", sizeof a );

    This will print the size of a pointer, not the size of the array, because the function arguments are passed in a value context. The array argument is converted to a pointer and the function expects a pointer parameter. This is also why the first dimension for an array parameter doesn't need to be specified (ie. int a[] vs. int a) and why the array notation and pointer notation are equivalent (eg. int a[] == int *a) as function parameters.

Votes + Comments
Was an eye opener

Suppose we take the example of following code:

char arr[] = "daniweb";
arr[2] = 'u';

In the second line above, neither we are retrieving and using the value (value context) nor we are modifying or querying the object itself(object context). Also we know that arr[2] = *(arr+2), that means the array is being converted to pointer here.... So which context is this...


>So which context is this...
You answered your own question, it's value context. Why? Because the subscript operator arr[2] is syntactic sugar for *(arr + 2). Clearly (arr + 2) is a subexpression which results in a value: the address offset by two elements. The indirection operator then takes that address value and retrieves the object that's required by the assignment operator.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.