当我编译我的C程序时,我得到了消息warning C4090: '=': different 'const' qualifiers。
我看过一些here,here,甚至here的信息。但我仍然不明白它们与我的问题有什么关系。对于编译,我使用Visual C++ 2015 x64本机构建工具命令提示符。我知道这是因为在函数声明中使用了const。但是数组并没有改变。那是怎么回事呢?
下面是我的代码:
#include <stdio.h>
int sum_array(const int a[], int n)
{
int *p, sum;
sum = 0;
for (p = a; p < a + n; p++)
sum += *p;
return sum;
}
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
printf("%d", sum_array(a, 5));
return 0;
}程序运行得很好,我只是想知道为什么我会收到这个警告。
发布于 2019-04-01 07:03:26
我很惊讶这只是一个警告。在C++中,这将是完全病态的。
您的函数接受一个const int*,然后将其分配给一个int*。
那不是const-correct。
我猜你是说const int *p??
https://stackoverflow.com/questions/55446215
复制相似问题