首页 > 其他 > 详细

[C]mecpy 和 memmove函数

时间:2020-02-06 11:51:24      阅读:82      评论:0      收藏:0      [点我收藏+]

#include <stdio.h>
#include <string.h> // for memcpy() and memmove()
#include <stdlib.h>
#define SIZE 10
//打印数组
void show_array(double ar[],int n)
{
    int index = 0;
    for (index=0; index<n; index++) {
        printf("%9.4f ",ar[index]);//每个数占9个 保留4位小数
        if(index%5==4)//每行6个
        {
            putchar('\n');
        }
    }
    if (index % 5 !=0) {//最后输出的时候换行
        putchar('\n');
    }
}

//伪随机生成数组
void fill_array(double ar[],int n)
{
    for (int index =0; index<n; index++) {
        ar[index] = (double)rand()/((double)rand()+0.1);
    }
}

int main()
{
    double darray[SIZE];
    fill_array(darray, SIZE);
    double target[SIZE];
    
    puts("original data:");
    show_array(darray, SIZE);
    
    puts("memcpy() used:");
    //memcpy按照字节进行copy
    memcpy(target, darray, SIZE*sizeof(double));
    puts("memcpy() result:");
    show_array(target, SIZE);
    
    char str[] = "memmove can be very useful......";
    memmove (str+20,str+15,11);
    puts (str);
    
    return 0;
}

Result:
技术分享图片

[C]mecpy 和 memmove函数

原文:https://www.cnblogs.com/tailiang/p/12267873.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!