What is the difference between these two C function calls?

I call the following library function in two ways:

unsigned int
LsSendToQ(unsigned int p4Node, const char queueName[4], lsMsg *pMsg,
          unsigned int prio) {

}

First way:

LsSendToQ((unsigned int)0, (const char *)Q_NAME, (lsMsg *)(void *)pMsg, 0) 

and the second way:

LsSendToQ((unsigned int)0, (const char *)Q_NAME, (lsMsg *)pMsg, 0) 

Both calls compile fine, but which one is correct? And why is it (void *)used in the first call, which looks like a pointer to a function?

+4
source share
3 answers

A pointer to voidis a "general" type of pointer. A void *can be converted to any other type of pointer without an explicit cast. You cannot play void *or do pointer arithmetic with it; you must first convert it to a pointer to a full data type. See this answer .

, pMsg lsMsg *, 2- , [ ].

, pMsg lsMsg *, 1- .

:

, 1-.

+3

.

. pMsg lsMsg *, , void* .

, ! , . , , (void*) " ". undefined .

+3

I see no reason for the first way to convert a pointer type twice. Just using the second method is enough.

+2
source

Source: https://habr.com/ru/post/1624901/


All Articles