Is this code variation messy or is it good?

I am taking Mosh’s C++ course and I stumbled upon this problem. So I am on the video where mosh compares pointers and I am on this exercise. Mosh did this code with a while loop instead of a for loop:

// Online C++ compiler to run C++ program online
#include <iostream>

using namespace std;

int main() {
    int numbers[] = {10, 20, 30};
    int* ptr = &numbers[size(numbers) - 1];
    
    for(int* ptr = &numbers[size(numbers) - 1]; ptr >= numbers; ptr--){
        cout << *ptr << endl;
    }
    
    


    return 0;
}

I did it with a for loop so it would kind of be a challenge for me. The goal is to use the pointer to print the numbers in the array in reverse order. It was a success, but I feel like this code is a bit messy. Is there anything I can adjust to make this code like a little shorter or maybe just cleaner. Please and thank you.

Hi.

Does it work ? → Then it’s good (enough).
Is there a simpler solution ? → Then refactor to simpler solution.

Now you say it is a personal challenge so focus on the 1st point.

Cheers.

1 Like