要動態分配內存的數組逆序,可以使用以下步驟:
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int *arr = (int*)malloc(n * sizeof(int));
printf("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int temp;
for (int i = 0; i < n/2; i++) {
temp = arr[i];
arr[i] = arr[n-1-i];
arr[n-1-i] = temp;
}
printf("Reversed array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
free(arr);
這樣就可以動態分配內存的數組逆序了。