要动态分配内存的数组逆序,可以使用以下步骤:
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);
这样就可以动态分配内存的数组逆序了。