Wednesday, August 31, 2022

[SOLVED] cast const pointers to void * in C++

Issue

I have the following code that does not compile with gcc 10.2.1 :

struct Bar {
    unsigned char *m_a;
    unsigned char m_b[1];
};

int main()
{
    Bar bar;
    const Bar &b = bar;

    void *p1 = b.m_a; // Ok
    void *p2 = b.m_b; // Error

    return 0;
}

The compiler error is :

error: invalid conversion from ‘const void*’ to ‘void*’ [-fpermissive]

I can fix this by using either void *p2 = (void *)b.m_b; or void *p2 = const_cast<unsigned char *>(b.m_b); however, constness of members does not seem to be treated the same by the compiler.

I guess there is an "extra-check" for the array and not with the pointer but why is that ?

Thank you.


Solution

Having a const struct adds const to all the members.

Adding const to unsigned char * gives you unsigned char * const (i.e., the pointer cannot be changed to point to anything else, but you can change the value of what is pointed to). This can be cast to void *, since that is also a pointer to non-const.

Adding const to unsigned char[1] gives you const unsigned char[1] (A const array of T is actually an array of const T). This can decay into a const unsigned char * pointer, which can be cast to a const void *, but not a void * without casting away the const. This is because the elements of the array cannot be modified, unlike the pointed-at object with the first member.



Answered By - Artyer
Answer Checked By - Mildred Charles (WPSolving Admin)