(void)变量

经常在一些驱动代码里面见到这种用法,变量前直接加(void)的语句,一开始对这种用法一头雾水,不知其何意!后经多方查阅资料了解,这只是一种防止编译器编译时报警告的用法。有些变量如果未曾使用,在编译时是会报错,从而有些导致编译不过,所以才会出现这种用法。而此语句在代码中没有具体意义,只是告诉编译器该变量已经使用了。

在freemodbus中遇到的这种用法:

eMBErrorCode
eMBRTUInit( UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
{
    eMBErrorCode    eStatus = MB_ENOERR;
    ULONG           usTimerT35_50us;

    ( void )ucSlaveAddress;
/*(void) tell compiler this variable has been used,dont warn!*/
    ENTER_CRITICAL_SECTION(  );

    /* Modbus RTU uses 8 Databits. */
    if( xMBPortSerialInit( ucPort, ulBaudRate, 8, eParity ) != TRUE )
    {
        eStatus = MB_EPORTERR;
    }
    else
    {
        /* If baudrate > 19200 then we should use the fixed timer values
         * t35 = 1750us. Otherwise t35 must be 3.5 times the character time.
         */
        if( ulBaudRate > 19200 )
        {
            usTimerT35_50us = 35;       /* 1750us. */
        }
        else
        {
         ...


猜你喜欢

转载自blog.csdn.net/qq_33611327/article/details/77770144