Data Pointers

Variable Address vs Variable Value

In some situations, we will want to work with a variable's address in memory, rather than the value it contains:

Pointers.png

A pointer is essentially a variable that holds the address of another variable or function. Pointers may be used to perform what is known in the assembly language world as indirect addressing. When you use pointers, you are able to access the variable that they point to and you are able to change the pointer so that it points to a different variable. This simple mechanism has hundreds of uses and is one of the most powerful features of the C programming language.

PointersF.png

A pointer allows us to indirectly access a variable.

PointersWork.png

Pointers make it possible to write a very short loop that performs the same task on a range of memory locations/variables.

Example

Data Buffer

RAM buffer allocated over a range of addresses (perhaps an array).

Pseudo-code:

1. Point arrow to the first address of the buffer.
2. Write data from the Universal Synchronous Receiver Transmitter (UART) to the location pointed to by the arrow.
3. Move the arrow to point to the next address in the buffer.
4. Repeat until data from the UART is 0 or buffer is full (arrow points to last address of buffer).

PointerExample.png

Pointers may be used in many ways. Some of the more common include being used in conjunction with dynamic memory allocation. A pointer may point to a location in the heap where a variable is created at runtime since the variable doesn't have a name in the same way as the variables we explicitly declare before compilation.

More frequently, you will see pointers used as a mechanism to pass multiple parameters to or from a function or as a way to pass a value BY REFERENCE to a function. Since C ordinarily passes variables to functions BY VALUE, the original variable is never changed. In some cases, we want to modify the original variable as a part of the function's operation. Therefore, we can pass a pointer to the function and it will then be able to modify the original variable (much more on this later). Pointers also provide an alternate means of accessing arrays which can be more efficient, especially when dealing with strings.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.