出来练习一下,看看能用几种方法
定义一个函数,将一维数组a中n个整数按相反顺序存放。在main函数中输入数组值,然后调用该函数,最后将反序存放的数组值输出。
程序代码:
#include<stdio.h> void reverse(int *p) { int *tmp = p; int n = 0, m = 0, i = 0, change; while (*tmp!=0) { tmp++; n++; } m = n>>1; for (; i<m; i++) { change = *(p+i); *(p+i) = *(p+n-i-1); *(p+n-i-1) = change; } } void print_array(int *p) { while (*p!=0) { printf("%d ",*p++); } printf("\n"); } void main() { int array[100] = {0}; int i = 0; for (; i<99; i++) { printf("please input a value: (0 means finish)\n"); scanf("%d",&array[i]); if (array[i]==0) { break; } } print_array(array); reverse(array); print_array(array); reverse(array); print_array(array); }