题目
【一维数组】交换数组
作业内容
将数组A中的内容和数组B中的内容进行交换。(数组一样大)
答案
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 0, b = 0, c = 0;
int arr1[3] = { 1,2,3 };
int arr2[3] = { 4,5,6 };
int sz = sizeof(arr1) / sizeof(arr1[0]);
for (int i = 0; i < sz; i++)
{
int tmp = arr1[i];
arr1[i] = arr2[i];
arr2[i] = tmp;
}
printf("数组arr1的内容: ");
for (int i = 0; i < sz; i++)
{
printf("%d ", arr1[i]);
}
printf("数组arr2的内容: ");
for (int i = 0; i < sz; i++)
{
printf("%d ", arr2[i]);
}
/*a=arr2[0];
arr2[0] = arr1[0];
arr2[0] = a;
b= arr2[1];
arr2[1] = arr1[1];
arr2[1] = b;
c=arr2[2] ;
arr2[2] = arr1[2];
arr2[2] = c;
printf("%d", *arr1);
printf("%d", *arr2);*/
return 0;
}
解析
在注释出,写明了一个令人思考的错误
具体打印的arr1为数组地址的解引用
故打印起来只有一个数字
标签:sz,数组,--,int,arr2,arr1,printf,一题 From: https://blog.csdn.net/Xian_haichi/article/details/142425280