值传递与地址传递的区别
#include <stdio.h>
int ArrayCopy(char* ori, char* cop, char Length)
{
char loop;
for(loop = 0; loop < Length; loop++)
{
*cop++ = *ori++;
}
return 0;
}
int main()
{
char original[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
char *copiedOne=original;
char copiedTwo[10];
char loop;
char Length;
Length = sizeof(original);
printf("元素数组 -> 拷贝后的数组 \n");
for(loop = 0; loop < sizeof(original); loop++)
{
printf(" %2d %2d\n", original[loop], copiedOne[loop]);
}
ArrayCopy(original,copiedTwo, Length);
copiedTwo[9] = 20;
printf("值传递:\n");
printf("值传递更改数据后,只有拷贝的数组中的元素被更改: \n");
printf("元素数组 -> 拷贝后的数组 \n");
for(loop = 0; loop < sizeof(original); loop++)
{
printf(" %2d %2d\n", original[loop], copiedTwo[loop]);
}
copiedOne[9] = 50;
printf("地址传递:\n");
printf("地址传递更改数据后,原数组元素与拷贝的数组中的元素同时被更改: \n");
printf("元素数组 -> 拷贝后的数组 \n");
for(loop = 0; loop < sizeof(original); loop++)
{
printf(" %2d %2d\n", original[loop], copiedOne[loop]);
}
return 0;
}
标签:char,区别,传递,地址,数组,printf,2d,original,loop
From: https://www.cnblogs.com/yesiming/p/17470190.html