How do arrays relate to pointers?


12.7 Array and Pointers

To avoid mistakes right away, arrays and pointers are not the same thing, even if they appear to be in the course of this chapter. A pointer is the address of an address, while an array name is just an address. This misconception that array and pointer are the same is often based on the fact that array and pointer declarations as formal parameters of a function are interchangeable, because here (and only here) an array breaks up into a pointer. However, this automatic "simplification" makes it difficult for a beginner to understand. This misunderstanding is exacerbated when a pointer to a memory block that dynamically reserves memory with malloc () is used like an array (since the memory block can also be used with []).


Internal

When the program starts, an array automatically occupies a memory area that can no longer be moved or changed in size. A pointer, on the other hand, has to be assigned a value so that it also points to an occupied memory. In addition, the "value" of a pointer can later be assigned to another "value" (memory object) at will. In addition, a pointer need not only point to the beginning of a memory block.


First, the following is an example of how pointers can be used to access an array:

/ * arr_ptr1.c * / #include #include int main (void) {int element [8] = {1, 2, 4, 8, 16, 32, 64, 128} ; int * ptr; int i; ptr = element; printf ("The value pointed to by * ptr is% d \ n", * ptr); printf ("With * ptr + 1 it now points to% d \ n", * (ptr + 1)); printf ("* (ptr + 3) =% d \ n", * (ptr + 3)); printf ("\ nNow all together: \ n"); for (i = 0; i <8; i ++) printf ("element [% d] =% d \ n", i, * (ptr + i)); return EXIT_SUCCESS; }

By the instruction

the address of the array element is passed to the pointer ptr. This works without the address operator, since according to the ANSI-C standard the array name is always viewed as a pointer to the first array element. Here is the proof and example of it:

/ * arr_ptr2.c * / #include #include int main (void) {int element [8] = {1, 2, 4, 8, 16, 32, 64, 128} ; int i; printf ("* element =% d \ n", * element); printf ("* (element + 1) =% d \ n", * (element + 1)); printf ("* (element + 3) =% d \ n", * (element + 3)); printf ("\ nNow all together: \ n"); for (i = 0; i <8; i ++) printf ("* (element +% d) =% d \ n", i, * (element + i)); return EXIT_SUCCESS; }

Unfortunately, it is precisely such program examples that give the impression that arrays and pointers are equivalent. I explained at the beginning why this is not the case.

If you absolutely want to use an address operator in the example shown above, you can also write it like this:

ptr = & element [0]; / * identical to ptr = element * /

The start address of the first element of the array with the index [0] is transferred to the pointer in both ways. The course of the program is now to be analyzed more closely.

With this instruction, the value 2 is output from the element array, ie element [1]. I would like to illustrate why this is so with the help of some graphics.

int * ptr; ... int element [8] = {1, 2, 4, 8, 16, 32, 64, 128};

Figure 12.14 Visual representation of the pointer and the array in memory

The array has the memory address 0022FF60 to 0022FF7C and a total size of 32 bytes (on 16-bit systems: 16 bytes). An element is four bytes in size, since int is four bytes in size (on 32-bit computers). Therefore the addressing is always done in steps of four. By the instruction

ptr = element / * or * / ptr = & element [0]

it looks like this in memory:

Figure 12.15 The pointer "ptr" refers to the first array element.

The pointer thus points to the first element in the array (or more precisely: to the memory address of the first element). Then with

the address 0022FF60 increased by four bytes. This also happens internally with the arrays when the index counter is increased.

So that the pointer actually points to the next address, ptr + 1 must be between brackets, because brackets have a higher binding force than the dereferencing operator and are therefore evaluated first. If you forget the brackets, you would not refer to the next address but to the value to which the pointer ptr points, and this is increased by one.

Now the pointer points ptr through * (ptr + 1):

Figure 12.16 The address of the pointer has been increased.

So the output would be 2. Now we come to the next statement:

This increases the value of the address to 0022FF6C. This is why the value 8 is also output:

Figure 12.17 After further increasing the address of the pointer

So to access the nth element of an array, you have the following options:

int array [10]; // declaration int * pointer1, * pointer2; pointer1 = array; // pointer1 to the start address of array pointer2 = array + 3; // pointer2 on 4th element of array array [0] = 99; // array [0] pointer1 [1] = 88; // array [1] * (pointer1 + 2) = 77; // array [2] * pointer2 = 66; // array [3]

The same applies to function calls to array names. You can declare an array parameter in functions in two ways:

int function (int elements []) // Equivalent to ... int function (int * elements)

So a function can be called with the following arguments:

int values ​​[] = {1, 2, 3, 5, 8}; int * pointer; pointer = values; function (values); // 1st possibility function (& values ​​[0]); // 2nd possibility function (pointer); // 3rd possibility

Of course it is also possible to pass the address of the nth element to a function:

function (& values ​​[2]); // address from the 3rd element to function

Here is a small example:

/ * arr_ptr3.c * / #include #include void function (int * array, int n_array) {int i; for (i = 0; i

But what about the runtime behavior? What happens when the function is used with field index?

void function (int array [], int n_array)

Compilers usually optimize the code themselves during translation. The conversion of a field index into a pointer is no longer a problem for the compiler these days. Thus, there should be no noticeable loss of run time when using the indexing operator.



your opinion

How did you like the Openbook? We always look forward to your feedback. Please send us your feedback as an e-mail to [email protected]