How to suppress warnings for 'void*' to 'foo*' conversions (reduced from errors by -fpermissive)

前端 未结 2 1940
执笔经年
执笔经年 2021-02-14 13:58

I\'m trying to compile some c code with g++ (yes, on purpose). I\'m getting errors like (for example):

error: invalid conversion from \'void*\' to \'unsigned cha         


        
2条回答
  •  情深已故
    2021-02-14 14:30

    How do I disable warnings (globaly) for conversions from void* to other pointer types?

    Well it might require direct interaction with compiler's code. This is probably not what you want. You want to fix your code. First, these are not warnings but errors. You need to cast void* as you can not convert a void* to anything without casting it first (it is possible in C however which is less type safe as C++ is).

    For example you would need to do

    void* buffer = operator new(100);
    unsigned char* etherhead = static_cast(buffer);
                                      ^
                                     cast
    

    If you want a dynamically allocated buffer of 100 unsigned char then you are better off doing this

    unsigned char* p = new unsigned char[100];
    

    and avoiding the cast.

    void pointers

提交回复
热议问题