C Primer Plus 练习 10-2

本文探讨了在程序中使用数组符号和指针复制数组的方法,通过实例代码展示了两种方式的实现过程,包括如何初始化数组、复制数组内容及输出数组元素。

题目:编写一个程序,初始化一个double数组,然后把数组内容复制到另外两个数组。第一个函数使用数组符号,第二个函数用指针,并使用指针增量符号。函数调用如下所示:

double source[5]={1.1,2.2,3.3,4.4,5.5};

double target1[5];

double target2[5];

copy_arr(source,target1,5);

copy_ptr(source,target2,5);

#include<stdio.h>
void show_arr(double *,int);
void copy_arr(double *,double *,int);
void copy_ptr(double *,double *,int); 
int main(void)
{
	double source[5]={1.1,2.2,3.3,4.4,5.5};
	double target1[5];
	double target2[5];
	int n;
	n=sizeof(source)/sizeof(source[0]);
	show_arr(source,n);
	copy_arr(source,target1,n); 
	copy_ptr(source,target2,n);
	
	return 0;
 } 
 void show_arr(double *source,int n)
{
	printf("source: \n");
	int i;
	
	for(i=0;i<n;i++)
	{
		printf("%4.1f",source[i]);
	}
	printf("\n");
}
 void copy_arr(double *source,double *target1,int n)
 {
 	printf("copy_arr: \n");
 	int i;
 	
 	for(i=0;i<n;i++)
 	{
 		target1[i]=source[i];
 		printf("%4.1f",target1[i]);
	 }
	 printf("\n");
 }
 void copy_ptr(double *source,double *target2,int n)
 {
 	printf("copy_ptr: \n");
 	int i;
 	
 	for(i=0;i<n;i++)
 	{
 		*(target2+i)=*(source+i);
 		printf("%4.1f",*(target2+i));
	 }
	 printf("\n");
 }


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值