user2149890
user2149890

Reputation: 9

Using memcpy() on a char **buffer

If I have a pointer to a message buffer how do I memcpy() into that buffer? 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.

Upvotes: 0

Views: 851

Answers (1)

David Heffernan
David Heffernan

Reputation: 612794

The pointer variable buffer does not point to anything. You need to allocate memory and make buffer point to it. For instance:

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

Upvotes: 2

Related Questions