memcpy和memmove

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define SIZE 10

void show_array (const int ar[], int n);
void show_arraydou (const double ar[], int n);

int main (void)
{
 int values[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 int target[SIZE];
 
 double curious[SIZE/2] = {1.0, 2.0, 3.0, 4.0, 5.0};
 
 puts ("memcpy() used: ");
 puts ("values (original data): ");
 
 show_array (values, SIZE);
 memcpy (target, values, SIZE * sizeof (int));
 puts ("target (copy of values): ");
 show_array(target, SIZE);
 
 puts ("/nUsing memmove () with overlapping ranges: ");
 memmove (values +2, values, 5 * sizeof (int));
 puts ("values -- elements 0-5 copied to 2-7: ");
 show_array (values, SIZE);
 
 puts ("/nUsing memcpy () to copy doubles to int: ");
 memcpy (target, curious, (SIZE / 2) * sizeof (double));
 puts ("target -- 5 doubles into 10 int positions: ");
 show_array(target, SIZE);
 puts ("double format:");
 show_arraydou ((double *)target, SIZE/2);
 
 return 0;
}

void show_array (const int ar[], int n)
{
 int i;
 
 for (i = 0;i < n; i++)
  printf ("%d ",ar[i]);
 putchar ('/n');
}

void show_arraydou (const double ar[], int n)
{
 int i;
 
 for (i = 0; i < n; i++)
  printf ("%.2f ",ar[i]);
 putchar ('/n');
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值