【问题标题】:Error: Invalid Use of Void Expression in C错误:在 C 中无效使用无效表达式
【发布时间】:2012-10-08 16:55:01
【问题描述】:

我收到关于以下函数的“无效使用无效表达式”错误:

void remove_data(char* memory, char* bitmap, int pos)
{
    int i;

    i = (int)test_bit(bitmap, pos-1);

    if(i != 0)
    {
        memory[pos-1] = NULL;
        toggle_bit(bitmap, pos-1);
    }
}

现在,根据我在其他线程上阅读的有关此类似问题的内容,当程序员在函数中使用将生成输出的内容时,会弹出此错误。但是,在我编写的代码中,我看不出是什么原因造成的。

编辑添加:

其中使用的toggle bit函数也是void类型,不返回值。

void test_bit(char * bitmap, int pos)
{
    int bit;

    bit = bitmap[pos/8] & (1<<(pos%8));
    printf("%d\n", &bit);
}

我在 test_bit 中注释了 printf 行,但错误仍然存​​在。

【问题讨论】:

  • 你到底从哪里得到错误,test_bittoggle_bit的声明是什么?
  • toggle_bit 似乎无关紧要,应该显示 test_bit 的定义
  • void test_bit 确实有一个 printf,我专注于在 remove_data 中找到我完全忘记检查它调用的函数的错误。
  • @Kali:你有你的答案......
  • 那么将无返回类型转换为int 的结果应该是什么?

标签: c


【解决方案1】:

但是,在我编写的代码中,我看不出是什么原因造成的。

我也是,因为我认为您没有发布相关代码。无论如何,我认为这可能是您的问题:

i = (int)test_bit(bitmap, pos-1);

请向我们出示test_bit 的签名。我猜它会返回 void 或者你已经在某处转发了它并且不小心写道:

void test_bit(char*, int);

由于该函数返回 void(即,什么都没有),因此您不能继续将返回值(什么都不是)转换为 int,这没有任何意义并且是非法的。

编辑:您已在 cmets 中验证 test_bit 实际上已声明返回 void,所以这是您的问题。

【讨论】:

    【解决方案2】:

    因为 test_bit 是无效的,你不能使用它的返回值(如i = (int)test_bit(bitmap, pos-1);)——那里没有返回值。

    【讨论】:

      【解决方案3】:

      你写的

       i = (int)test_bit(bitmap, pos-1);
      

      为什么要有演员表?

      函数的时候好像有点傻

      void test_bit(char * bitmap, int pos)
      {
          int bit;
      
          bit = bitmap[pos/8] & (1<<(pos%8));
          printf("%d\n", &bit);
      }
      

      你怎么能把 void 转换成整数?

      【讨论】:

      • 不必粗鲁。
      【解决方案4】:

      明显解决方法:不要赋值函数返回void的结果。

      Non-Obvious 解决方案:不要嵌套函数调用,这样你就不太可能了 寻找所有错误的地方,认为编译器坏了。

      #include <stdio.h> //:for: printf(...)
      
      void YOUR_MISTAKE_ACTUALLY_HERE();
      void  MyFunc( void );
      void* WRAPPED_MyFunc( int, int, int );
      
      
      int main( void ){
          printf("[BEG:main]\n");
      
      
          void* HW = NULL;
          HW = WRAPPED_MyFunc(  //:<---So you check this, but it looks fine.
              1
          ,   YOUR_MISTAKE_ACTUALLY_HERE()
          ,   3
          );; //:<--- compiler says "Invalid Use Of Void Expression" Here.
      
          if( HW ){ /** NOOP **/ };
      
      
          printf("[END:main]\n");
      } 
      
          void YOUR_MISTAKE_ACTUALLY_HERE(){ }
      
          //:Pretend this function is fetched from DLL.
          //:Casting all DLL functions initially to void(*pfn)void
          //:in a lookup table, then re-casting later.
          void MyFunc( void ){
      
              return;
      
          }//[;]//
      
          typedef  
              void* 
              (* MyFunc_TYPEDEF)(            
      
                  int a
              ,   int b
              ,   int c
      
              );
      
          void*
          WRAPPED_MyFunc(
      
              int a
          ,   int b
          ,   int c
      
          ){
      
      
                     MyFunc_TYPEDEF
              fun =( MyFunc_TYPEDEF)
                     MyFunc;
      
              void* ret=( fun(
      
                  a
              ,   b
              ,   c
      
              ));
      
              return( 
                  (void*)
                  ret 
              );;
      
          }//:Func
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-09
        • 1970-01-01
        • 2014-11-05
        • 1970-01-01
        • 2012-09-03
        • 1970-01-01
        相关资源
        最近更新 更多