数组反序输出 将数组a中n个整数按相反顺序存放,用指针变量作实参 输入说明: n个整数 输出说明: 按相反顺序存放后输出
时间: 2024-06-13 17:05:53 浏览: 85
用指向指针的方法对n个整数排序并输出。
以下是使用指针变量将数组反序输出的示例代码:
```c
#include <stdio.h>
void reverse(int *p, int n) {
int *p1 = p, *p2 = p + n - 1, temp;
while (p1 < p2) {
temp = *p1;
*p1 = *p2;
*p2 = temp;
p1++;
p2--;
}
}
int main() {
int n, a[100];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
reverse(a, n);
printf("The array has been inverted:\n");
for (int i = 0; i < n; i++) {
printf("%3d", *(a + i));
}
return 0;
}
```
输入示例:
```
5
1 2 3 4 5
```
输出示例:
```
The array has been inverted:
5 4 3 2 1
```
阅读全文