对于许多问题,答案似乎可以在"标准"中找到.但是,我们在哪里找到它?最好是在线.
谷歌搜索有时会觉得徒劳,尤其是对于C标准,因为他们在编程论坛的大量讨论中被淹没.
要开始这个,因为这些是我现在正在搜索的,那里有很好的在线资源:
指向非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限定符"在嵌套指针类型中丢弃限定符"?
我知道,从隐式转换char **到const char **无法做到的,为什么,而且转化为char *const *作品.请参阅底部以获取有关该说明的链接.
除了一件特别的事情外,这一切都是有道理的 所以我有以下代码:
#include <stdio.h>
void
print(const char *const*param)
{
printf("%s\n", param[0]);
}
int
main(int argc, char **argv)
{
print(argv);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我将其编译为C++代码,它编译得非常好.但是,如果相同的代码仅编译为C代码,我会收到错误(好吧,警告,但我们假设-Werror,即将警告视为错误).
GCC:
test.c: In function ‘main’:
test.c:12:11: warning: passing argument 1 of ‘print’ from incompatible pointer type [-Wincompatible-pointer-types]
print(argv);
^
test.c:4:1: note: expected ‘const char * const*’ but argument is of type ‘char **’
print(const char *const*param)
^
Run Code Online (Sandbox Code Playgroud)
铛:
test.c:12:11: warning: passing 'char **' …Run Code Online (Sandbox Code Playgroud) 我很好奇,为什么我不能传递char **给const char **函数?在传递char *给const char *函数的情况下,使用双指针执行它似乎不行.我认为添加常量总是可以的(但不能删除常量)但现在看来我错了.
Gcc编译器给我错误的错误:
note: expected ‘const char **’ but argument is of type ‘char **’
Run Code Online (Sandbox Code Playgroud)
这是代码片段:
int f(const char **a) { }
int main() {
char *a;
f(&a);
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我有以下文件aaa.c:
int Test(const int * const * int_array, int count) {
int min = int_array[0][0];
for (int i = 1; i < count; ++i) {
if (min > int_array[i][0])
min = int_array[i][0];
}
return min;
}
void test_Test(void) {
int i[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int *pi[10] = { &i[0], &i[1], &i[2], &i[3], &i[4], &i[5],
&i[6], &i[7], &i[8], &i[9] };
Test(pi, 10);
}
Run Code Online (Sandbox Code Playgroud)
当我编译它时gcc -c aaa.c,会产生以下警告:
% …Run Code Online (Sandbox Code Playgroud) 在C中,const char *p有时称为"只读"指针:指向常量对象的指针(在本例中为a char).
看起来似乎也是如此
const char **pconst char *const *p将是指向指针的只读指针的等效声明,具体取决于间接的多少级别是不可变的.
但是,编译器(gcc,clang)会生成警告.
我的问题:如何在char **p没有生成警告的情况下将指针(如)作为"只读"指针传递给函数?如果需要明确的演员表,为什么char **p不是char *p?
这是我想要实现的具体例子.
此代码视为char *ptr只读指针.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void readonly(const char *ptr)
{
// ... do something with ptr ...
// but modifying the object it points to is forbidden
// *ptr = 'j'; // error: read-only variable is not assignable
}
int main(void)
{ …Run Code Online (Sandbox Code Playgroud) 为什么这段代码会给我一个警告:从不兼容的指针类型传递"test"的参数1?我知道这是关于char之前的const,但为什么呢?
void test(const int ** a)
{
}
int main()
{
int a=0;
int *b=&a;
int **c=&b;
test(c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)