What's this usage of the variable cast to void in function body?

一世执手 提交于 2020-01-03 06:51:32

问题


I am just learning to code embedded C. I see some code like below.

The function is defined like this:

void printDebug(const char d1[]){(void)d1;}

And it is used like this:

printDebug("BLE_UART_EVENT");

I don't understand its purpose. It gives me an impression of a callable char array?


回答1:


It's not calling char array, it's just explicitly converting the char array to void. (And the evaluated result is discarded immediately.)

I think it's just used to prohibit the compiler warning of unused variables.

If the parameter won't be used at all, it would be clearer to make it an unnamed parameter.

void printDebug(const char[]) {}


来源:https://stackoverflow.com/questions/39307858/whats-this-usage-of-the-variable-cast-to-void-in-function-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!