What should be the correct way to assign a value to a non-emtpy void* pointer in C?

I've the following code:

void funcA(void*   pArg)
{
    STRUCTA abc;
    .
    . // Some processing here
    .

    if (pArg)
       (STRUCTA *)pArg = abc;
}

the problem is, this code is throwing up the following warning: warning: target of assignment not really an lvalue; this will be a hard error in the future

Without the cast, I'll get another warning that I'm trying to dereference a void pointer...

As warnings are being treated as errors, I can't use this code - But I really can't use any other pointer type than void* as the argument. Is there an elegant solution I'm missing ?

Is there any way to make this work ?

Answers


You are assigning a STRUCTA to a pointer to STRUCTA.

Rather do:

*((STRUCTA *)pArg) = abc;

(STRUCTA *)pArg is of pointer type, while abc isn't. You need to dereference the pointer:

*(STRUCTA *)pArg = abc;

Need Your Help

O(log n) algorithm to find best insert position in sorted array

arrays algorithm sorting linked-list

I'm trying to make an algorithm that finds the best position to insert the target into the already sorted array.

Is there an alternative to Dockerfile Env Instruction?

docker

In their official site (https://docs.docker.com/reference/builder/#env), Docker support state that: