2

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.

vetional
  • 21
  • 1
  • 2
  • Is this question about C or about C++? Please remove either of those tags as arrays act differently in those very different languages. –  Oct 07 '11 at 16:50
  • 2
    It seems to decay any time you sneeze in its general direction. – David Thornley Oct 07 '11 at 16:50
  • I don't really understand what you're trying to ask here. Edit your questionto be more clear. – Chris Lutz Oct 07 '11 at 16:52
  • Answer here for C++: [How do I use arrays in C++?](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) – R. Martinho Fernandes Oct 07 '11 at 16:52
  • @WTP: While the languages are different, most of C90 works the same in C++ and C99 as it used to. I don't know of any differences with arrays and array-to-pointer conversions. Most of the differences are in usage, stuff you'd do differently in C and C++, and this isn't a usage question. – David Thornley Oct 07 '11 at 16:58
  • 1
    possible duplicate of [Is array name a pointer in C?](http://stackoverflow.com/questions/1641957/is-array-name-a-pointer-in-c) – Chris Lutz Oct 07 '11 at 17:00
  • @David: One difference is that in c++ arrays can be passed by reference into functions, retaining their size information. – Benjamin Lindley Oct 07 '11 at 17:01
  • The existing question cited by Chris Lutz has an excellent summary in the accepted answer. – Andy Thomas Oct 07 '11 at 17:29

5 Answers5

13

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.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • @potrzebie In the expression `a++`, `a` converts to a pointer. The result of the conversion is an rvalue, and the `++` operator requires an lvalue. – James Kanze Nov 29 '12 at 18:20
1

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..

Nawaz
  • 353,942
  • 115
  • 666
  • 851
1

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 that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (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 keyword static 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.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0
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]
phoxis
  • 60,131
  • 14
  • 81
  • 117
-3

Arrays in C are basically just pointers that reserve consecutive blocks of memory. So in essence arrays always act like pointers.

Toomai
  • 3,974
  • 1
  • 20
  • 22