OiO.lk Blog C++ Is it legal to pass a const pointer to a struct and a pointer to its member as non-const?
C++

Is it legal to pass a const pointer to a struct and a pointer to its member as non-const?


typedef struct
{
    int a;
    int b;
} my_struct_t;

void f2(const my_struct_t *my_struct, int *b)
{
    *b = my_struct->a + 1;
}

void f1(my_struct_t *my_struct)
{
    f2(my_struct, &(my_struct->b));
}

The my_struct parameter in f2() is declared as const so none of its members can be modified, but the b parameter is a pointer to a member of my_struct which is modified.

I wonder if, according to the c99 standard, the way I called f2() is legal or could lead to undefined behavior?



You need to sign in to view this answers

Exit mobile version