Jan 30, 2011 at 8:42pm UTC
So i have this array
int a[] = {45,67,89,4,3,101};
and need to just revers it to
int a[]= {101, 3, 4, 89, 67, 45};
using no [] and only pointers.
This is for C we aren't allowed to use C++ syntax.
Can anyone help? I have the basic idea that i have to swap each element by swapping their address using points just not sure how to!
Thanks!
Jan 30, 2011 at 9:40pm UTC
Just use a normal reverse algorithm, just replacing all instances of the subscript operator with pointer arithmetic.
array[n] == *(array + n)
Jan 30, 2011 at 9:43pm UTC
I can't use [] anywhere in it except just declaring the array
Jan 30, 2011 at 9:57pm UTC
Zhuge was just trying to show two equivalent bits of code, and he's right! Instead of the subscript operator, one can just use some basic pointer arithmetic and a dereference operator.
*(array + n)
is the same as array[n]
.
EDIT: Typo! Gah! :)
Happy coding!
-Albatross
Last edited on Jan 30, 2011 at 10:02pm UTC
Jan 30, 2011 at 10:00pm UTC
oh ok perfect works now:D
Thanks!