Functions, Pointers and Arrays

  1. Because the hardware instructions of computing machines rely heavily on addresses, pointers enable you to express yourself in a way that is close to how the machine expresses itself. This correspondence makes programs with pointers efficient. In particular, pointers offer an efficient way to deal with arrays. Indeed, as you will see, array notation is simply a disguised use of pointers. An example of this disguised use is that an array name is also the address of the first element of the array. 注意:数组名是一个常量,不是变量.

  2. You can use a pointer to identify an individual element of an array and to obtain its value. In essence, you have two different notations for the same thing. Indeed, the C language standard discribes array notation in terms of pointers. That is, it defines ar[n] to mean *(ar + n). You can think of the second expression as meaning, "Go to memory location ar, move over n units, and retrieve the value there."

        Function definition with arrays:

int sum(int *ar, int n);
int sum(int *, int);
int sum(int ar[], int n);
int sum(int [], int);

        The form int * ar always means that ar is type pointer-to-int. The form in ar[] also means that ar is type  pointer- to-int, but only when used to declare formal parameters. The idea is that the third form reminds the reader that not only does ar point to an int, it points to an int that't an element of an array.

3.     C guarantees that when it allocates space for an array, a pointer to the first location after the end of the array is a valid pointer. By the way, although C guarantees that the pointer marbles + SIZE is a valid pointer, it makes no gurantees about marble[SIZE], the value stored at that location.


你可能感兴趣的:(Functions, Pointers and Arrays)