將a[0]與a[n-1]對換,再將a[1]與a[n-2]對換…直到將a[int(n-1)]與a[int((n-1)/2)-1]對換。數組
如圖所示:
spa
//數組實現逆置 void conver_arr(int c,int a[]) { int low =0; int high = c -1; for(int i =0; i < c/2; i++) { if(low < high) { int temp = a[c - i -1]; a[c - i -1]= a[i]; a[i]= temp; } } }
//指針實現逆置 void conver_point(int c,int*a) { int*start; start = a; int*end; end= a + c -1; while(start <end) { int*temp =*start; *start =*end; *end= temp; *start++; *end--; } }
//打印輸出信息 void arr_print(int*a) { for(int i =0; i <10; i++) { printf("a[%d] = %d\n", i+1, a[i]); } } int main(void) { int arr_value[10]={12,34,5,67,3,54,6,31,46,1}; int len =sizeof(arr_value)/sizeof(arr_value[0]); conver_arr(len, arr_value); conver_point(len, arr_value); arr_print(arr_value); return0; }
運行結果以下圖所示:
3d