c语言函数修改数组 c语言数组调换

C语言如何在函数中修改数组,并且原数组不改变#include stdio.h
【c语言函数修改数组 c语言数组调换】int add(int *a)
{
int i = 0;
int sum = 0;
for( i = 0; i100; i)
{
sum= (*(a i))*2;
}
return sum;
}
int main(){
int a[100]={1,2,3};
//result
printf("The add result is %d.\n",add(a));
//print the array
printf("{");
for(int i = 0; i100; i)
printf("%d, ",a[i]);
printf("}");
return 0;
}
sum result is 12.
{1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }
Press ENTER or type command to continue
写一个函数修改数组中的数据,在主函数中调用它 。(C语言)?#includestdio.h
// 用于修改数组c语言函数修改数组的函数
int change_array(int *num, int count)
{
int i,t;
for(i = 0; icount; i)
{
t = num[i];
num[i] = t1;
}
}
int main()
{
int i, b[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// 调用函数
change_array(b, 10);
// 输出结果
for(i = 0; i10;i)
{
printf("%d ", b[i]);
}
}
c语言在调用函数中改变数组 , 原数组改变吗C语言不会复制整个数组,它会要求传入数组名(一个指向数组首地址的const指针) , 函数中对整数的改变其实就是对实际数组的改变!
关于c语言函数修改数组和c语言数组调换的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读