I have a function to which I will pass a struct ID and it will return me a string.
I will pass a pointer to store the name string and that pointer will be allocated memory by the function called.
int ConvertIDToName(void *id, void *name, size_t *size)
{
int status = 0;
unsigned char *xIDname = "user4.microsoft.com";
*name = (unsigned char*)malloc(30);
memcpy(*name, xIDName, *size);
...
return(0);
}
main(int argc, char* argv[])
{
struct ID_t idObj ={1,5, {0,0,0,0,0,5}};
unsigned char* IDName = NULL;
UINT32 IDNameSize = MAX_CHAR;
ConvertIDToName(&idObj, &IDName, (size_t *)&IDNameSize);
return(0);
}
The function ConvertIDToName() fails to store the allocated memory address in the void pointer.
I am unable to assign memory in the pointer and it gives me error from 3rd statement of function ConvertIDToName():
warning: dereferencing �void *� pointer
test_code.c:683: error: invalid use of void expression
What am I doing wrong?
---------- Post updated at 02:33 AM ---------- Previous update was at 02:32 AM ----------
I missed the simple thing of typecasting the void pointer before using it.
This solves the issue:
*(unsigned char **)name = malloc(30);
memcpy(*(unsigned char **)name, xIDName, *size);