memcpy的缓冲区和数组不工作 [英] memcpy a buffer and an array not working

查看:249
本文介绍了memcpy的缓冲区和数组不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在,我需要传递一个空数组作为参数传递给函数的要求。并在此调用的函数,我应该有一些数据memcpy的到传递的数组。所以我写了一个小例子是一样的我的要求。下面是它的code:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
无效printArr为(int *一)
{
    INT I;
    为int * B =(INT *)malloc的(的sizeof(int)的* 10);
    的printf(\\ n \\ n输入10 lements:\\ n);
    对于(I = 0; I&小于10;我+ +)
        scanf函数(%D,和b [i]);    的printf(数组b \\ nContents:\\ n);
    对于(I = 0; I&小于10;我+ +)
        的printf(%d个\\ t的,B [I]);
    的printf(\\ n);
    的memcpy(A,B,10);
    的printf(数组a \\ nContents:\\ n);
    对于(I = 0; I&小于10;我+ +)
        的printf(%d个\\ t的,一个[我]);
    的printf(\\ n);
}
诠释的main()
{
    int类型的[10];
    printArr(一);
    返回0;
}

在上面的例子中,即时发送从主功能的printArr函数数组。现在,在调用功能时,数据将是的memcpy到阵列。当数组内容打印我得到一些垃圾值。还汇编发出警告,如下所示:

  $ gcc的-o ARR array.c
array.c:在函数'printArr:
array.c:15:2:警告:内建函数不兼容的隐式声明的memcpy

时,如以下所示的上述程序的输出:

 进入10 lements:
0 1 2 3 4 5 6 7 8 9数组b的内容:
0 1 2 3 4 5 6 7 8 9数组的内容:
0 1 134479874 11136160 11136160 11132916 134514160 134513696 134514171 11132916

有人可以告诉我什么是错在上面的程序。

注意:我需要将数据从缓冲区传递的数组复制只能用memcpy和不通过的,因为在我的实际程序性能的原因循环

先谢谢了。


解决方案

 的memcpy(A,B,10);

第三个参数是字节数进行复制。你想的memcpy(A,B,* 10 * sizeof的一个)


另外,你缺少一个的#include<文件string.h方式> ,这就是为什么你会得到警告

I have a requirement in which i need to pass an empty array as a parameter to a function. And in this called function, i should be memcpy some data into the passed array. So i have written a small example which is same as my requirement. Below is its code:

#include <stdio.h>
#include <stdlib.h>
void printArr(int *a)
{
    int i;
    int *b=(int*)malloc(sizeof(int)*10);
    printf("\n\nEnter 10 lements:\n");
    for(i=0;i<10;i++)
        scanf("%d",&b[i]);

    printf("\nContents of array b:\n");
    for(i=0;i<10;i++)
        printf("%d\t",b[i]);
    printf("\n");
    memcpy(a,b,10);
    printf("\nContents of array a:\n");
    for(i=0;i<10;i++)
        printf("%d\t",a[i]);
    printf("\n");
}
int main()
{
    int a[10];
    printArr(a);
    return 0;
}

In the above example, i'm sending an array from the main function to the printArr function. Now in the called function, data will be memcpy into the array. When the array contents are printed i get some junk values. Also the compilation gives a warning as shown below:

$ gcc -o arr array.c
array.c: In function ‘printArr’:
array.c:15:2: warning: incompatible implicit declaration of built-in function ‘memcpy’

The output of the above program is as shown below:

Enter 10 lements:
0 1 2 3 4 5 6 7 8 9

Contents of array b:
0   1   2   3   4   5   6   7   8   9   

Contents of array a:
0   1   134479874   11136160    11136160    11132916    134514160   134513696   134514171   11132916    

Can someone please tell me what's wrong in the above program.

Note: I need to copy data from the buffer to passed array only using memcpy and not via a for loop because of performance reasons in my actual program.

Thanks in advance.

解决方案

memcpy(a,b,10);

The third argument it the number of bytes to copy. You want memcpy(a, b, 10 * sizeof *a).


Also, you are missing an #include <string.h>, that's why you get the warning.

这篇关于memcpy的缓冲区和数组不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆