Вопрос

Is there way to create an const int** from an int**?

I'm currently using:

const int **pixel2=*pixel;

const char **header2=*header;

I keep getting the error: cscd240HW34.c:52:21: warning: initialization from incompatible pointer type [enabled by default] const int **pixel2=*pixel;

Это было полезно?

Решение

If pixel is already of type int ** then:

const int **pixel2 = (const int **)pixel;

By way of explanation: the reason a cast is required is because this still doesn't give you as much type-safety as you might think. For example you could now write:

const int c = 'x';
*pixel2 = &c;    // fine, both are const int *
**pixel = 'y';   // no compiler error, but UB as we modify a non-writable object

So, see if there is another way to do what you want to do. Note that this definition of pixel2 avoids that exploit

const int * const * pixel2;

although sadly C still requires a cast to assign pixel to pixel2.

This question is 11.10 in the c.l.c. FAQ.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top