Array of Pointers in C++

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Array of Pointers to Strings

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
Your next lesson will play in 10 seconds
  • 0:04 Review of Arrays
  • 1:06 Array of Pointers
  • 1:58 Re-Ordering Values
  • 2:37 Characters
  • 3:05 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

An array is a list or holding tank for a set of values. Once those values are set it's very hard to re-order them or add new items. However, we can use an array of pointers instead. This lesson will cover the concept of using pointers in an array and provide working code examples.

Review of Arrays

Let's take a moment before we start in earnest on this lesson to recap the important information about arrays. An array is a single element with multiple components. Think of it as a list of names, phone numbers, or other information. You can think of each item in the list as a bucket within that list. Remember, an array's buckets start counting with zero! Therefore, the first bucket of any array is 0, the second 1, and so forth.

Here's a basic integer array:

#include <stdio.h>
#include <iostream>
using namespace std;
const in MAXIMUM = 5;
int main() {
  int var[MAXIMUM] = {3, 18, 12, 32, 16)};
  for(int i = 0; i < MAXIMUM; i++) {
   cout << "Variable[" << i << &quot] = "
   cout << var[i] << endl;
  return 0;

The output would look like:

C basic array output

This type of array is common, and it works well in many situations. However, once the array is set, it's very difficult to change. What if you want to add new values to the array, or switch the order? It requires a lot of additional programming and frustration because of the way the array has been written. What's the solution? Create an array of pointers. They point to the items in the array. Then all we have to do is switch, add, change, modify the pointers!

Array of Pointers

A pointer holds an address to something. It's kind of like an address because it points to a specific value. Check out the following diagram showing a pointer payCode pointing to a value of 507.

C Pointers Graphic

To declare a pointer, we use the asterisk in front of the variable name, like the following:

int *ptr[MAXIMUM];

Before we look at a re-do of the prior code, we need to take a closer look at how we're working with the pointers and the addresses. When we process the array, there's an extra line of code that assigns the address/pointer to our original variable. Omitting this step is the most common error in pointer code.

ptr[i] = &myvar[i];

Now let's look at the full code. It's still very similar to the original run through, but we're using and displaying the pointers instead of the direct value of each bucket.

#include <stdio.h>
#include <iostream>
using namespace std;
const in MAXIMUM = 5;
int main() {
  int var[MAXIMUM] = {3, 18, 12, 32, 16)};
  int *ptr[MAXIMUM];
  for(int i = 0; i < MAXIMUM; i++) {
   ptr[i] = &myVar[i];
   //use the address
  for(int i = 0; i < MAXIMUM; i++) {
   "Value of myvar[" << i << "] = "

When the program runs, the following output is displayed:

C pointers array output

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account