zhugean 发表于 2010-6-12 11:05:16

请教C语言强制类型转换的一个问题

有这样一个函数
void func(unsigned char a)
{
    (void) a;
}
请问里面的(void)是什么意思?
实在搞不懂啊,求教了:)

newc 发表于 2010-6-12 11:30:17

只是一个类型为空的变量,应该是占用一个字节的空间。。好像是这样的。

tomhe666 发表于 2010-6-12 11:52:46

(void *)是有的,void型的变量真的没见过

liaowei 发表于 2010-6-12 11:56:24

看看那个编译器会放过你再说

voidx 发表于 2010-6-12 11:59:30

变量被访问了,但什么都没做。
通常用来欺骗编译器。

void func(unsigned char a)
{
   //变量a没有被使用,有些编译器会有警告
}

void func(unsigned char a)
{
    (void) a;   //变量a什么都不做,但表示被访问过了,编译器不会有警告
}

void func(volatile unsigned char a)
{
    (void) a;   //读一次变量a,但读出来的值不做任何处理。
}

zhugean 发表于 2010-6-12 12:38:59

voidx真是高手啊,学习了,谢谢
贴出问题的出处,给大家参考
eMBErrorCode
eMBRTUInit( UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
{
    eMBErrorCode    eStatus = MB_ENOERR;
    ULONG         usTimerT35_50us;

    ( void )ucSlaveAddress;
    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;       /* 1800us. */
      }
      else
      {
            /* The timer reload value for a character is given by:
             *
             * ChTimeValue = Ticks_per_1s / ( Baudrate / 11 )
             *             = 11 * Ticks_per_1s / Baudrate
             *             = 220000 / Baudrate
             * The reload for t3.5 is 1.5 times this value and similary
             * for t3.5.
             */
            usTimerT35_50us = ( 7UL * 220000UL ) / ( 2UL * ulBaudRate );
      }
      if( xMBPortTimersInit( ( USHORT ) usTimerT35_50us ) != TRUE )
      {
            eStatus = MB_EPORTERR;
      }
    }
    EXIT_CRITICAL_SECTION();

    return eStatus;
}

ucSlaveAddress确实没有用到:)

zhugean 发表于 2010-6-12 12:46:24

再请教voidx一下,如果(void)func();是什么意思呢

zhugean 发表于 2010-6-12 12:49:08

程序是这样的:
if( xMBPortEventGet( &eEvent ) == TRUE )
    {
      switch ( eEvent )
      {
      case EV_READY:
            break;

      case EV_FRAME_RECEIVED:
            eStatus = peMBFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength );
            if( eStatus == MB_ENOERR )
            {
                /* Check if the frame is for us. If not ignore the frame. */
                if( ( ucRcvAddress == ucMBAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) )
                {
                  ( void )xMBPortEventPost( EV_EXECUTE );
                }
            }
            break;
}

snoopyzz 发表于 2010-6-12 13:39:48

操作的如果是寄存器,可能有特殊作用。有些特殊寄存器被读取时,会清/置某些标志位

king3306 发表于 2012-12-23 14:18:27

5楼高手也

n0831 发表于 2012-12-23 14:39:21

曾强制转换过函数返回值,变量还真不知道。5楼高手,学习!

jz701209李 发表于 2013-4-10 15:46:53

学习一下....
页: [1]
查看完整版本: 请教C语言强制类型转换的一个问题