C 那个函数参数真的是指向常量的指针吗?

C 那个函数参数真的是指向常量的指针吗?,c,pointers,clang-tidy,C,Pointers,Clang Tidy,在下面的C程序中,函数f、g和h基本相同,但是clang tidy说参数p可以指向g和h中的常量(但在f中不能)。我的理解是,p不能在其中任何一个中指向const。这是假阳性吗 结构{int*p;}; 外部无效x(结构S*S); 空f(int*p) { 结构; s、 p=p; x&s; } void g(int*p) { 结构S={.p=p}; x&s; } 空h(int*p) { 结构S={p}; x&s; } int main() { int a=0; f&a; g&a; h&a; } 来

在下面的C程序中,函数
f
g
h
基本相同,但是clang tidy说参数
p
可以指向
g
h
中的常量(但在
f
中不能)。我的理解是,
p
不能在其中任何一个中指向const。这是假阳性吗

结构{int*p;}; 外部无效x(结构S*S); 空f(int*p) { 结构; s、 p=p; x&s; } void g(int*p) { 结构S={.p=p}; x&s; } 空h(int*p) { 结构S={p}; x&s; } int main() { int a=0; f&a; g&a; h&a; } 来自clang tidy的输出:

$ clang-tidy --checks=* a.c
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "a.c"
No compilation database found in /home/wolfram or any parent directory
fixed-compilation-database: Error while opening fixed database: No such file or directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.
2 warnings generated.
/home/wolfram/a.c:14:13: warning: pointer parameter 'p' can be pointer to const [readability-non-const-parameter]
void g(int *p)
            ^
       const 
/home/wolfram/a.c:20:13: warning: pointer parameter 'p' can be pointer to const [readability-non-const-parameter]
void h(int *p)
            ^
       const 
尝试使用叮当整洁的版本10和11


以下内容可能与此相关:

该工具存在错误。此警告的目的可能是强制执行“const correction”,这仅在指针实际在函数中被取消引用时才起作用。您不需要反引用指针

但更重要的是,C语言要求对指针进行简单赋值(6.5.1.6.1),重点是:

  • 左操作数具有原子、限定或非限定指针类型,以及 左值转换后左操作数的类型)两个操作数都是 指向兼容类型的合格或不合格版本的指针,以及指向的类型 左边的to具有右边所指类型的所有限定符

使指针参数
const
将变为
s.p=p和类似的约束冲突-无效C。初始化规则也遵循简单赋值规则,因此各种函数的行为相同。

如果将调用顺序更改为
f
g
h
,会发生什么情况?或者为每个调用提供不同的参数(即不同整数的地址)?@AdrianMole,这不会改变任何内容。我甚至可以完全删除主功能,结果是一样的。看起来绝对像叮当作响的tidy bug。在这两个函数中,此参数都不能是指向const的指针。尝试一下,将其更改为指向常量的指针-在丢弃常量限定符的赋值时,您可能会收到另一个警告。您引用的错误看起来正是这个问题-它可能在解析结构初始化时遇到一些问题。似乎你回答了你自己的问题。作为一个叮当作响的整洁虫报告给LLVM Bugzilla: