Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is array name a pointer to the first element of the array?

Tags:

arrays

c

pointers

Is this always the case , i mean , that array name is always a pointer to the first element of the array.why is it so?is it something implementation kinda thing or a language feature?

like image 438
chanzerre Avatar asked Aug 29 '13 18:08

chanzerre


People also ask

Why is array name a pointer to the first element?

An array name contains the address of first element of the array which acts like constant pointer. It means, the address stored in array name can't be changed. For example, if we have an array named val then val and &val[0] can be used interchangeably.

Why array is a pointer?

An array is a pointer, and you can store that pointer into any pointer variable of the correct type. For example, int A[10]; int* p = A; p[0] = 0; makes variable p point to the first member of array A.

Why the memory address of array name is equal to the memory address of the first element of an array?

An array is a sequence of one or more elements of a given type, while a pointer points to a memory location (which may be the first element of an array). Because of this, the address of an array is the same as the address of the first element.

What is the first element in an array called?

The first element in every array has index zero and is sometimes called the zeroth element. Thus, the elements of array c are c[0] , c[1] , c[2] and so on.


2 Answers

An array name is not itself a pointer, but decays into a pointer to the first element of the array in most contexts. It's that way because the language defines it that way.

From C11 6.3.2.1 Lvalues, arrays, and function designators, paragraph 3:

Except when it is the operand of the sizeof operator, the _Alignof 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.

You can learn more about this topic (and lots about the subtle behaviour involved) from the Arrays and Pointers section of the comp.lang.c FAQ.

Editorial aside: The same kind of behaviour takes place in C++, though the language specifies it a bit differently. For reference, from a C++11 draft I have here, 4.2 Array-to-pointer conversion, paragraph 1:

An lvalue or rvalue of type "array of N T" or "array of unknown bound of T" can be converted to an rvalue of type "pointer to T". The result is a pointer to the first element of the array.

like image 102
Carl Norum Avatar answered Sep 18 '22 12:09

Carl Norum


The historical reason for this behavior can be found here.

C was derived from an earlier language named B (go figure). B was a typeless language, and memory was treated as a linear array of "cells", basically unsigned integers.

In B, when you declared an N-element array, as in

auto a[10];

N cells were allocated for the array, and another cell was set aside to store the address of the first element, which was bound to the variable a. As in C, array indexing was done through pointer arithmetic:

a[j] == *(a+j)

This worked pretty well until Ritchie started adding struct types to C. The example he gives in the paper is a hypothetical file system entry, which is a node id followed by a name:

struct {
  int inumber;
  char name[14];
};

He wanted the contents of the struct type to match the data on the disk; 2 bytes for an integer immediately followed by 14 bytes for the name. There was no good place to stash the pointer to the first element of the array.

So he got rid of it. Instead of setting aside storage for the pointer, he designed the language so that the pointer value would be computed from the array expression itself.

This, incidentally, is why an array expression cannot be the target of an assignment; it's effectively the same thing as writing 3 = 4; - you'd be trying to assign a value to another value.

like image 20
John Bode Avatar answered Sep 19 '22 12:09

John Bode