简体   繁体   English

在char **缓冲区上使用memcpy()

[英]Using memcpy() on a char **buffer

If I have a pointer to a message buffer how do I memcpy() into that buffer? 如果我有一个指向消息缓冲区的指针,我如何memcpy()进入该缓冲区? For example say I have the following: 例如,说我有以下内容:

char **buffer;
char data[10]

memcpy(*buffer, data, 10);

But this doesn't seem to work and always crashes my program, however the compiler doesn't see to mind. 但这似乎不起作用,总是崩溃我的程序,但编译器没有想到。 Can someone please tell me why? 有人可以告诉我为什么吗? Btw the reason I have a char **buffer is because its being passed as a parameter of the function. 顺便说一下我有一个char **缓冲区的原因是因为它作为函数的参数传递。

The pointer variable buffer does not point to anything. 指针变量buffer不指向任何内容。 You need to allocate memory and make buffer point to it. 您需要分配内存并使buffer指向它。 For instance: 例如:

buffer = malloc(sizeof(*buffer));
*buffer = malloc(10);
memcpy(*buffer, data, 10);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM