如何从int **创建const int **

时间:2014-03-09 20:51:37

标签: c const double-pointer

有没有办法从int **?

创建一个const int **

我目前正在使用:

const int **pixel2=*pixel;

const char **header2=*header;

我一直收到错误:cscd240HW34.c:52:21: warning: initialization from incompatible pointer type [enabled by default] const int **pixel2=*pixel;

1 个答案:

答案 0 :(得分:1)

如果pixel已经是int **类型,那么:

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

作为解释:需要演员表的原因是因为这仍然没有给你那么多的类型安全性。例如,你现在可以写:

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

那么,看看是否有其他方法可以做你想做的事情。请注意,pixel2的此定义可以避免利用

const int * const * pixel2;

虽然遗憾的是C仍需要演员才能将pixel分配给pixel2

这个问题在c.l.c中是11.10。常见问题。