ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Arrays of Pointers in C Plus Plus

Updated on March 17, 2010

Arrays of pointers in C Plus Plus

This tutorial introduces basic concepts and examples illustrating arrays of pointers in C Plus Plus. The topic is commonly referred to as double indirection by programmers.

Overview of the concept

A array in C Plus Plus as an ordered list of homogeneous data items. An array can be indexed by a non-negative integer value, starting at zero and proceeding to 1 less than the declared length of the array. Arrays are very common programming tools, but most arrays consist of simple data types such as integers, floats, or characters. Thus tutorial addresses arrays with an identical structure but comprised instead of memory addresses.

A memory address, commonly referred to as a pointer, describes a specific location in the physical or logical memory space (this tutorial does not discuss physical vs concepts). The structure of a memory address varies across operating systems and processors, but C Plus Plus is designed to be a portable language at the source code level, so all our examples should compile on most popular platforms.

A simple example in code

Consider the following code example:

// Introduction to arrays of pointers
#include <iostream>
using namespace std;	// .Net code needs this, gcc not so much

void main()
	int *alpha;
	int myArray[10];
	int *myArrayOfPointers[10];

Overview of the example code

Our example code demonstrates 4 different pointers.

  • Line 7 declares a pointer to an integer.
  • Line 8 also declares a pointer to an integer, but myArray is a pointer constant and cannot be overwritten.
  • Line 9 declares an array of pointers to integers and a pointer constant called myArrayOfPointers.

This tutorial is primarily focused on the declaration in line 7. Each element of the array is a pointer to an integer. Contrast this line with line 6, where each element of the array is an integer. The difference is significant. An integer and a pointer to an integer are two completely different data types. The array called myArrayOfPointers still consists of 10 elements and can be indexed in the same manner as any other array, but care must be taken to manipulate the individual elements properly.

In the following code module we extend the code that was introduced above:

// Introduction to arrays of pointers
#include <iostream>
using namespace std;	// .Net code needs this, gcc not so much

void main()
	int *alpha;
	int myArray[10];
	int *myArrayOfPointers[10];

	myArrayOfPointers[0] = 42; // probably won't compile
	myArrayOfPointers[0] = (int*)42; // a type cast saves the day


Lines 11 and 12 were added to the previous code example. Line 11 illustrates a technique that worked very well in the original C language, but probably won't compile in C++ (C Plus Plus). The intention of line 11 is to initialize the first element of the array to the value 42. This value is assumed to be a memory address; since the array, myArrayOfPointers, is an array pointers to integers, anything we store in the array should be the memory address of an integer. We are assuming that our program has permission to access the information stored at memory address 42; that assumption cannot be validated until the program is compiled, linked, and executed, but the assumption is important to understand as the code is being analyzed.

If line 11 does not compile,and it probably will not, line 12 will solve the problem through the application of a type cast. The type cast is a syntax that tells the compiler to convert the integer 42 to an equivalent memory address. To many readers this type cast seems redundant because any programmer with embedded systems development experience recognizes the expression '42' as a memory address even before the application of the type cast. C Plus Plus is happy to convert the integer value 42 to a memory address format as long as the type cast syntax is present. Without the type cast, the compiler will generate at best a syntax warning and probably a syntax error.

Dereferencing the pointer in the array

Given that we have successfully declared an array of pointers to integers and we have assigned a value to one or more array elements, we can now apply the deference operator to make use of the pointer. The following code illustrates how to do this:

// Introduction to arrays of pointers
#include <iostream>
using namespace std;	// .Net code needs this, gcc not so much

void main()
	int *alpha;
	int myArray[10];
	int *myArrayOfPointers[10];

	myArrayOfPointers[0] = 42; // probably won't compile
	myArrayOfPointers[0] = (int*)42; // a type cast saves the day

	*myArrayOfPointers[0] = 100; // Store 100 at address 42
	cout << "\n" << *myArrayOfPointers[0];	// prints 100


Line 14 demonstrates the syntax for dereferencing the pointer stored in the array of pointers. We are storing the integer value 100 at memory address 42.The data currently stored at memory address 42 will be overwritten by this operation. The data currently stored at addresses 43, 44, and 45 will also be replaced, assuming 4 byte integers. If the target CPU uses 2 byte integers,  then only data at addresses 42 and 43 will be overwritten. The format of the integer will be a function of the integer format required by the target CPU.

Line 15 prints the value stored at memory address 42. There is no format specifier in the cout statement. Note that the cout object 'knows' what data type to print based on the declaration in line 9.


Arrays of pointers can be used to directly manipulate memory. The syntax used to create and employ arrays of pointers is supported by C++ (C Plus Plus).


    0 of 8192 characters used
    Post Comment

    No comments yet.


    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at:

    Show Details
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the or domains, for performance and efficiency reasons. (Privacy Policy)
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)