문제

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?

도움이 되었습니까?

해결책

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;

Try this:

memcpy(pArg, &abc, sizeof(abc));

However you must make sure that pArg points to sizeof(abc) bytes of allocated memory.

Maybe pArg = (void *) abc; ?

EDIT:

pArg = (void *) (&abc); ?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top