我已经尝试使用指针重新排列数组中的数字,但我实际上已经实现了它,但是我以一个可怕的代码结束了,我知道可能有更好的方法来做到这一点,弄明白了。我只想在我的代码上输入内容。 另外我知道我的整数的名字不是最好的,所以请不要评论我。使用指针重新排列数组中的数字
#include <iostream>
using namespace std;
void Fill(int a[], int b) {
for (int i = 0; i < b; i++)
*(a + i) = rand() % 100;
}
void Print(int a[], int b) {
for (int i = 0; i < b; i++)
cout << *(a + i) << " ";
}
void swap(int a[], int b, int c[]) {
for (int i = 0; i < b; i++) {
*(c + (b - i - 1)) = *(a + i);
}
for (int i = 0; i < b; i++) {
*(a + i) = *(c + i);
}
for (int i = 0; i < b; i++) {
cout << *(a + i) << " ";
}
}
int main() {
int hello1[10], goodbye[10];
Fill(hello1, 10);
Print(hello1, 10);
cout << endl;
swap(hello1, 10, goodbye);
cin.get();
cin.get();
return 0;
}
你为什么不只是使用指数,这是同样的事情更短的形式! –