简体   繁体   English

错误:在 C 中无效使用无效表达式

[英]Error: Invalid Use of Void Expression in C

I'm getting an "invalid use of void expression" error regarding the following function:我收到关于以下函数的“无效使用无效表达式”错误:

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);
    }
}

Now from what I've read on other threads regarding this similar issue this error pops up when the programmer uses something within the function that will generate an output.现在,从我在其他线程上读到的有关此类似问题的内容中,当程序员在函数中使用将生成输出的某些内容时,会弹出此错误。 However, I'm failing to see what would cause that in the code I've written.但是,我没有看到在我编写的代码中会导致什么。

Edit to add:编辑添加:

The toggle bit function used in it is also type void and doesn't return a value.其中使用的切换位函数也是 void 类型并且不返回值。

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

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

I commented the printf line in test_bit but the error is still there.我在 test_bit 中评论了 printf 行,但错误仍然存​​在。

However, I'm failing to see what would cause that in the code I've written.但是,我没有看到在我编写的代码中会导致什么。

Me too, because I don't think you have posted the relevant code.我也是,因为我认为您没有发布相关代码。 Regardless, I think this is probably your problem:无论如何,我认为这可能是你的问题:

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

Please show us the signature for test_bit .请向我们展示test_bit的签名。 I'm guessing it returns void or you have forward declared it somewhere and accidentally wrote:我猜它会返回void或者您已经在某处转发并意外写道:

void test_bit(char*, int);

Since the function returns void (ie, nothing) you cannot then proceed to cast the return value (which is nothing) to an int as you are doing, it makes no sense and is illegal.由于该函数返回void (即没有),因此您不能继续将返回值(即无)转换为int ,因为这是没有意义的并且是非法的。

EDIT: You have verified in the comments that test_bit is in fact declared to return void , so that's your problem.编辑:您已在评论中验证test_bit实际上已声明返回void ,所以这是您的问题。

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

You wrote你写了

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

Why have a cast?为什么要有演员表?

Seems a bit wrong when the function is功能时似乎有点错误

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

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

You can't convert a void into an integer.您不能将 void 转换为整数。

Obvious Solution: Don't assign results of function returning void.明显的解决方案:不要分配函数返回 void 的结果。

Non-Obvious Solution: Don't nest function calls, and you are less likely to be looking in all of the wrong spots, thinking the compiler is broken.非显而易见的解决方案:不要嵌套函数调用,并且您不太可能会查看所有错误的位置,认为编译器已损坏。

#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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM