I wanted to copy the content of a two dimensional array into another two dimensional char array. I used the following for
loop with memcpy
but it is not working as desired. So I have two questions.
- What is wrong with this code? and
Is there a way to do it without use of iteration?
for (int i = 0; i < count; i++) { memcpy(&buf_copy[i], buf[i], sizeof(buf[i])); }
Both buf
and buf_copy
are 2d dynamic char arrays.
Edit: declarations of the arrays
char **buf;
char **buf_copy;
EDIT 2: Here is how memory is allocated to them
void intit_buf()
{
buf = (char**)malloc(BUFFER * sizeof(*buf));
for (int i = 0; i < BUFFER; i++)
buf[i] = (char*)malloc(sizeof(char) * 33);
//initialize buf_copy
buf_copy = (char**)malloc(BUFFER * sizeof(*buf_copy));
for (int i = 0; i < BUFFER; i++)
buf_copy[i] = (char*)malloc(sizeof(char) * 33);
}