马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 小蒙 于 2017-11-1 10:37 编辑
#include <stdio.h>
#include <ctype.h>
void pp(double *p, int num);
void copy_arr(double target[], double source[], int num);
void copy_ptr(double *target, double *source, int num);
void copy_ptrs(double *target, double *start, double *end);
int main(void) {
double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
double target2[5];
double target3[5];
double target4[5];
// for (int i = 0; i < 5; ++i) {
// printf("%p\n", &target4[i]);
// }
copy_arr(target2, source, 5);
copy_ptr(target3, source, 5);
copy_ptrs(target4, source, source + 5);
printf("target2:");
pp(target2, 5);
printf("target3:");
pp(target3, 5);
printf("target4:");
pp(target4, 5);
return 0;
}
void copy_arr(double target[], double source[], int num) {
for (int i = 0; i < num; ++i) {
target[i] = source[i];
}
}
void copy_ptr(double *target, double *source, int num) {
for (int i = 0; i < num; ++i) {
*target++ = *(source + i);
}
}
void copy_ptrs(double *target, double *start, double *end) {
for (; target < end;) {
*target++ = *start++;
}
}
void pp(double *p, int num) {
for (int i = 0; i < num; ++i) {
printf("%.1f ", *p++);
}
putchar('\n');
}
如图打印结果是
如果注释掉其他两条函数调用只打印任意一个函数,结果就是对的。
本帖最后由 橙C 于 2017-11-1 12:31 编辑
改成这样..就符合你的要求..
void copy_ptrs(double *target, double *start, double *end) {
for (; start< end;) {
*target++ = *start++;
}
}
|