Hello,
I'd like to fill a buffer with different type on variables (i.e. integers, strings..).
I have a pointer to pointer data structure (char **array_of_indexed_buffers) that initialized as follows:
array_of_indexed_buffers = malloc(numOfPackets * sizeof(char *));
if(g_array_of_indexed_buffers == NULL)
{
perror("malloc");
exit(1);
}
for packet each entry, I have the following allocation.
for (i = 0; i < g_numOfPackets; i++)
{
/* allocate some memory for a buffer pointed by an array in index i */
array_of_indexed_buffers[i] = malloc(MAX_RX_TX_BUFFER_LENGTH);
if (array_of_indexed_buffers[i]==NULL)
{
perror("malloc");
exit(1);
}
/* erase content of allocated buffer */
bzero(array_of_indexed_buffers[i], MAX_RX_TX_BUFFER_LENGTH);
/* get piece of data from the big chunk received/created buffer and insert header parameters and bufferSize data into each array_of_indexed_buffers[i]*/
insertHeder((char *)(received_buf+(i * bufferSize)), i);
}
Now the problem I have is with the filling of each packet first with "index", then with bufferSize, and eventually with a data which i take from received_buf (data size is bufferSize)
void insertHeder
(
char *src_data_element,
int index
)
{
char *p;
bzero(g_array_of_indexed_buffers[index], MAX_RX_TX_BUFFER_LENGTH);
p = (char *)array_of_indexed_buffers[index];
*p = index ;
p = p + (sizeof (int*));
*p = (int)g_bufferSize;
p = p + (sizeof (int*));
memcpy(p, src_data_element, g_bufferSize);
}
the problem is I can't fill each packet in that way (with p pointer) to make it pointed by array_of_indexed_buffers[index]
Please suggest me how can I do this.
Thank you in advance,
Alex