Print array in reverse order using pointer

I have the necessary knowledge and code to successfully print an array in order however, I'm getting my butt kicked trying to get the same array printed in reverse order. Any help would be appreciated as I've already gone through videos and books and (obviously) none have helped. Thanks, in advance, for your time and input.

So, my question is, how do I go about printing the array of an unknown size (based on user input), in reverse order?

1
2
3
4
5
6
7
void displayArray(int *arrayStart, //<-- first element of array
                  int *arrayEnd  ) //<-- last  element of array

for (int *ptr = arrayStart; ptr <= arrayEnd; ptr++)
		cout << *ptr << " " << endl;
                     // ^ print array in order
Last edited on
Well, you could just start at the end and go backwards, its not too hard:

1
2
3
4
void displayArray(int *arrayStart, int *arrayEnd) {
    for (int *ptr = arrayEnd; ptr >= arrayStart; ptr--)
        std::cout << *ptr << std::endl;
}
Oh my gawd... I really want to slap myself. I tried that very same thing (almost!) only I forgot to flip the "less-than" operator. Thank you so much for checking my head, NT3! It really helps to have another set of eyes and brain. Cheers, mate!
Last edited on
Topic archived. No new replies allowed.