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 ).
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:
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:
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:
The start address of the first element of the array with the index  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 . I would like to illustrate why this is so with the help of some graphics.
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
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:
The same applies to function calls to array names. You can declare an array parameter in functions in two ways:
So a function can be called with the following arguments:
Of course it is also possible to pass the address of the nth element to a function:
Here is a small example:
But what about the runtime behavior? What happens when the function is used with field index?
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.
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]
- Why are the Welsh famous for singing?
- What are some good schools in Gurgaon
- Why has youth sports become so expensive
- Components for the website
- What is online learning and offline learning
- Who discovered phloem
- Why are passengers served sweets?
- Is there scope in parapsychology
- What is the requirement to get into NIT
- Why is juuling so attractive to teenagers?
- What is an unconscious mind
- What can I do after a PharmD
- Why is it so easy to sin?
- How effective is google maps in driving
- Beef is heart healthier than steak
- Is Google biased towards Donald Trump
- How can I heal my pain
- What happens if the RNA polymerase malfunctions
- What font style does this blog use
- Is that a false statement
- What makes super glue a super glue
- Will the internet ever break
- Banks will count your change
- Prayers alleviate our problems