CJCoding With Joseph
15free / daySign inCreate account

Mastering C++

Pointer-Based Arrays

Mastering Pointers

So far, we’ve used pointers to work with single values. But one of the most common and powerful uses of pointers in C++ is with arrays.

Let’s take a step back for a moment. In C++, when you create an array like int nums[5];, you’re asking the program to reserve space for 5 integers. This memory is stored on the stack, and the size must be known ahead of time when you write the code.

But what if you don’t know how many items you need until the program is running? Or what if the user should decide the size?

That’s where pointer-based arrays come in. You can use a pointer to create an array on the heap using new[], which gives you full control over the size and lifetime of the array.

Members only

Keep reading this lesson

You've reached the end of the free preview. Unlock the rest of this lesson plus 21 more lessons in Mastering C++.

  • Unlimited coding questions
  • Unlimited access to all classroom material
  • Weekly live call for questions and guidance
  • 3,000 AI tutor tokens / month
  • Personal SQL database (3 MB)
  • Progress tracking

Cancel any time.