Arrays Of Pointers

Declaration

An array of pointers is an ordinary array variable whose elements happen to be all pointers.

This creates an array of four pointers to char. The array p[] itself is like any other array. The elements of p[], such as p[1], are pointers to char.

Initialization

Initializing an element of an array of pointers is just like initializing ordinary pointers, but we must include the array index. This example assigns the address of x to the pointer in element of the array.

This next example assigns the address of the first character in the string to the pointer in element 0 of the array.

This example shows how you would populate the array of pointers in the following picture.

ArraysOfPointers.png

This would create the strings in memory and initialize each of the pointers in the array to point to a particular string.

Dereferencing

To use the value pointed to by a pointer array element, just dereference it like you would an ordinary variable:

Using *p[0] is the same as using the object it points to, such as x or the string literal "My String" from before.

Accessing Strings

Example

© 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.