I'm currently learning C++ and wrote an Array Reverse Function for learning purposes only.
Everything works fine. But if i want to write back my value from my stack into my array.. it fails.
#include "iostream"
#include <stack>
using namespace std;
void reverseArray(int *a, int s) {
stack<int> stack;
register int i;
for (i = 0; i < s; i++) { // iterates from 0 to 5
stack.push(*a);
a++; // pointer adress + 4 byte
}
for (i = 0; i < s; i++) { // iterates from 0 to 5
a = &stack.top(); // this fails!!
printf("%i\n", *a); // Here ist the right output
stack.pop();
a++; // pointer adress + 4 byte
}
}
int main() {
const int SIZE = 5;
int array[SIZE] = {1, 2, 3, 4, 5};
reverseArray(&array[0], SIZE);
printf("This should be 5: %i\n", array[0]);
return 0;
}
This creates following output:
5
4
3
2
1
This should be 5: 1