指向非const数据的指针可以隐式转换为指向相同类型的const数据的指针:
int *x = NULL;
int const *y = x;
Run Code Online (Sandbox Code Playgroud)
添加额外的const限定符以匹配额外的间接寻址应该在逻辑上以相同的方式工作:
int * *x = NULL;
int *const *y = x; /* okay */
int const *const *z = y; /* warning */
Run Code Online (Sandbox Code Playgroud)
-Wall但是,使用标志对GCC或Clang进行编译会产生以下警告:
test.c:4:23: warning: initializing 'int const *const *' with an expression of type
'int *const *' discards qualifiers in nested pointer types
int const *const *z = y; /* warning */
^ ~
Run Code Online (Sandbox Code Playgroud)
为什么添加额外的const限定符"在嵌套指针类型中丢弃限定符"?
我想CFStreamCreatePairWithSocketToHost用NSInput- 和OutputStreams.我有两个伊娃NSInputStream *_inputStream和NSOutputStream *_outputStream.
以下给出了两条错误消息:
CFStreamCreatePairWithSocketToHost(NULL,
(__bridge_retained CFStringRef)self.hostname, self.port,
(CFReadStreamRef *)&_inputStream, (CFWriteStreamRef *)&_outputStream);
Run Code Online (Sandbox Code Playgroud)
错误:一个间接指针的浇铸到一个Objective-C指针"
CFReadStreamRef *"(又名"struct __CFReadStream **')是不允许与ARC
错误:一个间接指针的浇铸到一个Objective-C指针’CFWriteStreamRef *"(又名"struct __CFWriteStream **")是不允许的同ARC
我该如何解决这个问题?我试过使用__bridge但是我得到了类似的错误消息.
objective-c core-foundation multiple-indirection foundation automatic-ref-counting
我有一个类,其中一个二维数组的int实现为int**.我按如下方式为这个2D数组实现了一个访问器函数,返回一个const int**以防止用户编辑它:
const int** Class::Access() const
{
return pp_array;
}
Run Code Online (Sandbox Code Playgroud)
但是我得到了编译错误"从int**到const int**的无效转换".为什么不允许在这里推广const?如何在不编辑权限的情况下授予用户访问信息的权限?