0

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.

2
  • 4
    before writing memory, you need to allocate. also theres nothing to stop you passing a char * into a function.. Commented Nov 16, 2013 at 0:23
  • buffer points nowhere specified and *buffer points nowhere specified. Writing to the unknown is undefined behaviour. Commented Nov 16, 2013 at 0:25

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.