|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
拷贝数组:#include<stdio.h>
void show_arr(const double ar[], int n);
void copy_arr(double target[],const double source[], int n);
void copy_ptr(double * target,const double * source, int n);
void copy_ptrs(double * target, double * source, const double * ar_end);
int main(void){
double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
double target1[5];
double target2[5];
double target3[5];
copy_arr(target1, source, 5);
copy_ptr(target2, source, 5);
copy_ptrs(target3, source, source + 1);
show_arr(target1, 5);
show_arr(target2, 5);
show_arr(target3, 5);
return 0;
}
void show_arr(const double ar[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%-5g", ar[i]);
}
putchar('\n');
}
void copy_arr(double target[],const double source[], int n){
int idx;
for (idx = 0; idx < n; idx++){
target[idx] = source[idx];
}
}
void copy_ptr(double * target,const double * source, int n){
int idx;
for (idx = 0; idx < n; idx++){
*(target + idx) = *(source + idx);
}
}
void copy_ptrs(double * target, double * source, const double * ar_end){
int count = 0;
double * ar_start = target;
while(ar_start < ar_end){
count++;
*ar_start++ = *source++;
}
printf("copy_ptrs %d 次\n", count);
}
编译运行:copy_ptrs 19 次
0 0 0 0 0
1.08555e-3126.95139e-3106.95123e-3106.95139e-3100
1.1 2.2 3.3 4.4 5.5
Process finished with exit code 0
show_arr和show_ptr问题出在哪?
为什么copy_ptrs里面while循环了19次?
本帖最后由 jhq999 于 2022-10-11 17:02 编辑 #include<stdio.h>
void show_arr(const double ar[], int n);
void copy_arr(double target[],const double source[], int n);
void copy_ptr(double * target,const double * source, int n);
void copy_ptrs(double * target, double * source, const double * ar_end);
int main(void){
double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
double target1[5];
double target2[5];
double target3[5];
copy_arr(target1, source, 5);
copy_ptr(target2, source, 5);
copy_ptrs(target3, source, &source + 1);//////////&source + 1才是source的结尾
show_arr(target1, 5);
show_arr(target2, 5);
show_arr(target3, 5);
return 0;
}
void show_arr(const double ar[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%-5g", ar[i]);
}
putchar('\n');
}
void copy_arr(double target[],const double source[], int n){
int idx;
for (idx = 0; idx < n; idx++){
target[idx] = source[idx];
}
}
void copy_ptr(double * target,const double * source, int n){
int idx;
for (idx = 0; idx < n; idx++){
*(target + idx) = *(source + idx);
}
}
void copy_ptrs(double * target, double * source, const double * ar_end){
int count = 0;
double * ar_start = source;///////target3和source没有关系,两个数组,
while(ar_start < ar_end){///////如果 ar_start= target,会有越界的情况,我这个。16次
count++;
*target++ = *ar_start++;////////
}
printf("copy_ptrs %d 次\n", count);
}
copy_ptrs 5 次
1.1 2.2 3.3 4.4 5.5
1.1 2.2 3.3 4.4 5.5
1.1 2.2 3.3 4.4 5.5
Process returned 0 (0x0) execution time : 0.237 s
Press any key to continue.
或者 copy_ptrs(target3, source, &target3+ 1);
void copy_ptrs(double * target, double * source, const double * ar_end){
int count = 0;
double * ar_start = target;
while(ar_start < ar_end){
count++;
*ar_start++ = *source++;
}
printf("copy_ptrs %d 次\n", count);
}
|
|