Printing array backwards
Why's this program throwing me an error? I could've sworn this is the way to print an array backwards.
1 2 3 4 5 6 7 8 9 10 11
|
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
int a[4] = {10, 20, 30, 40};
for(int i = a.length() - 1; i >= 0; i--) cout << a[i] << " ";
return 0;
}
|
Last edited on
length()
is a member function. That could work with for example a std::string.
But the plain array doesn't have any such functions.
You could instead use:
|
const int length = sizeof(a) / sizeof(a[0]);
|
and then use
length
rather than
a.length()
in your code.
Topic archived. No new replies allowed.