I am trying to copy data from a buffer which i know its length to a char[] starting from a given index, the problem is that the data contains null, so the program crashes for a segmentation fault error.
Here is a sample of my code:
char *tmp = list->at(0); //list->at(0) return a pointer to the data
char *pEnd = tmp;
for (i = 0; i<size;i++)
{
buffer[i] = *pEnd ; //<<<-----here I got the segmentation fault
pEnd++;
}
If you are saying that
list->at(0)returnsNULLThen the pointerpEndwill beNULL.Therefore doing this
*pEndis de-referencing a NULL pointer which will obviously seg fault.If you want to chek for this, you could check the pointer before de-referencing. for eg: