【发布时间】:2018-03-24 14:20:07
【问题描述】:
这个应用程序应该使用下面的函数交换数组的前三个数字。当我交换像 1 2 3 4 5 这样的数字时,代码可以工作,但是当我尝试使用这个数字 5 3 4 9 8 7 2 时,它不会显示正确的输出。我真的找不到代码有什么问题。
#include <stdio.h>
int main(void){
int a_lenth, i,a_content;
printf("Enter the lenth of the array:");
scanf("%d", &a_lenth);
int arr[a_lenth];
int arr2[a_lenth];
for(i = 0; i < a_lenth; i++){
printf("Enter the elements of the array:");
scanf("%d", &a_content);
arr[i] = a_content;
arr2[i] = a_content;
};
roll(arr, a_content, arr2);
return 0;
}
void roll(int *a1, int n, int *a2){
int e;
a2[0] = a1[2];
a2[1] = a1[0];
a2[2] = a1[1];
for(e = 0; e < n; e++){
printf("%d\n", a2[e]);
}
}
【问题讨论】:
-
这个调用roll(arr, a_content, arr2);中a_content是什么意思;
-
a_content 是从用户那里获取值并将其存储在数组 arr[0] = a_content 中的变量。
-
如果一次还不足以理解我的问题,请再读一遍。
-
如果我理解正确,n 应该是数组的长度。那为什么函数调用
roll是用a_content完成的呢?不应该用a_lenth完成吗?
标签: c arrays function pointers swap