就在你的函數改變array[size]
到array
代碼[打印後到前]:
#include <stdio.h>
int print(int array[],int size) {
if(size>0) { // changed this too
printf(" %d",array[size-1]);
return print(array,size-1); // note this carefully.
}
}
int main() {
int arr[]={1,4,6,9,0,3};
print(arr, sizeof(arr)/sizeof(int)); // changed to correct size [sizeof is generic than just mentioning in the size]
return 0;
}
代碼[先打印到最後]
#include <stdio.h>
int print(int array[],int size) {
if(size>0) { // changed this too
print(array,size-1); // note this carefully.
printf(" %d",array[size-1]);
}
}
int main() {
int arr[]={1,4,6,9,0,3};
print(arr, sizeof(arr)/sizeof(int)); // changed to correct size [sizeof is generic than just mentioning in the size]
return 0;
}
根據OP的要求,說明它首先如何運作。
數學說明: 讓print (arr,size)
是將0打印到大小數組的功能。 現在print(arr,size+1)
將是 print(arr,size); printf(arr[size])
現在再次查看代碼。
直觀地說,如果您首先打印最後一頁,則必須先打印較低的元素,最後打印最高的元素。
如果看到原始代碼,則首先打印了第n個元素,然後是反向打印。
傳遞元件陣列。改變這一點,並命令一切都應該罰款 – qwr
如果所有推薦的警告被啓用,你應該得到一個編譯器警告。你爲什麼忽略它? – Olaf