I can't seem to figure out what is wrong with my code below:
I have a double pointer char
array, declared and initialized with the following:
unsigned char **buffer = (unsigned char**) malloc (num * sizeof(char*));
for ( i = 0; i < num; i++ )
{
buffer[i] = (unsigned char*) calloc(PACKETSIZE, sizeof(char));
}
Then I'm trying to copy a string to the middle section of one of the char*
arrays, but it does not seem to work. I'm not sure whether my error was with the memory allocation or when I tried to copy. I know for a fact the source char*
has content.
The code I'm trying to copy (Header is a struct, I want to write to the array after the memory address of Header for buffer[i]
, so I'm doing a bit of a pointer arithmetic).
strncpy ((unsigned char *)(buffer[i]+sizeof(Header)), buffer2, bytes_to_copy);
After the code runs, the buffer[i]
stays empty.
Here is a sample of the Header struct:
typedef struct Head
{
unsigned int x;
unsigned int y;
} Header ;