i know that the array name could be used as a pointer(although it\'s a converted form), but my question is , Is there some other instance where array acts as a pointer.
It is due to array-to-pointer standard conversion. Specifically, array decays into a pointer to the first element of the array.
Array decays into a pointer, I think, because arrays and pointers are used exactly in the same way: using an index, which you can increment and decrement, to traverse the elements, in both direction, forward and backward..
The identifier itself tells the base address of the memory block.
int arr[SIZE];
arr
+------+------+---- ----+------+
| | | . . . | |
+------+------+---- ----+------+
arr[0] arr[1] arr[2] arr[n-1] arr[n]
The `arr' holds the address of the base address of the block
int *arr = malloc (sizeof (int) * SIZE);
arr
+------+
|addr1 |------------+
+------+ |
addr_of_arr |
|
|
V
+------+------+---- ----+------+
| | | . . . | |
+------+------+---- ----+------+
addr1[0] addr[1] addr1[n-1] addr1[n]
Arrays in C are basically just pointers that reserve consecutive blocks of memory. So in essence arrays always act like pointers.
Here are the relevant sections of the C language standard (you asked for C, that's what you get):
6.3.2.1 Lvalues, arrays, and function designators
...
3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.
...
6.5.2.1 Array subscripting
Constraints
1 One of the expressions shall have type ‘‘pointer to object type’’, the other expression shall have integer type, and the result has type ‘‘type’’.
Semantics
2 A postfix expression followed by an expression in square brackets[]
is a subscripted designation of an element of an array object. The definition of the subscript operator[]
is thatE1[E2]
is identical to(*((E1)+(E2)))
. Because of the conversion rules that apply to the binary+
operator, ifE1
is an array object (equivalently, a pointer to the initial element of an array object) andE2
is an integer,E1[E2]
designates theE2
-th element ofE1
(counting from zero).
...
6.7.5.3 Function declarators (including prototypes)
...
7 A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, where the type qualifiers (if any) are those specified within the[
and]
of the array type derivation. If the keywordstatic
also appears within the[
and]
of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as specified by the size expression
The important thing to take away from this is that there is a difference between an object (in the C sense, something that takes up memory) and the expression we use to refer to that object. Arrays are always arrays, but the expression used to refer to that object will often be of a pointer type.
Technically speaking, an array name never acts as a pointer. An expression with array type (which could be an array name) will convert to a pointer anytime an array type is not legal, but a pointer type is. And the declaration of an array as a function parameter is converted into a declaration of a pointer. (Which means that the name isn't an array name, but a pointer name. Despite appearances.)