Printing an array in C++?

JohnHemmars picture JohnHemmars · Sep 2, 2009 · Viewed 397.9k times · Source

Is there a way of printing arrays in C++?

I'm trying to make a function that reverses a user-input array and then prints it out. I tried Googling this problem and it seemed like C++ can't print arrays. That can't be true can it?

Answer

Botz3000 picture Botz3000 · Sep 2, 2009

Just iterate over the elements. Like this:

for (int i = numElements - 1; i >= 0; i--) 
    cout << array[i];

Note: As Maxim Egorushkin pointed out, this could overflow. See his comment below for a better solution.