Pointers and Arrays

How can I print the array backwards with the pointer already there?
instead of it printing out 1,2,3,4,5,6,7,8,9,10
I can't figure out how to print it out backwards. 10 through 1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <iostream>
using namespace std;
 int main ()
{
    int *p;
    int ar[]= {1,2,3,4,5,6,7,8,9,10};
    p=&ar[10];
    

    for ( int i=0; i<10;i++)
    
        cout << *p[&ar[10]-1] << endl


    return 0;
}
for ( int i=0; i<10;i++)

See how this goes from 0 to 9? Make one that goes from 9 to 0.
p=&ar[9];


for ( int i=10; i>0;i--)

cout << *p--<< endl;


so something like this...
1
2
3
4
5
6
7
 int *p;
int ar[]= {1,2,3,4,5,6,7,8,9,10};
p = ar;
for ( int i=9; i>0;i--)
{
  cout << p[i] << endl;
}


You don't need the extra pointer, but since you want one anyway, there it is.
Last edited on
Topic archived. No new replies allowed.