简体   繁体   中英

Can't understand “warning C4090: '=': different 'const' qualifiers” in my c program

I've got message warning C4090: '=': different 'const' qualifiers when compiling my C program.

I've seen some info here , here and even here . But I still don't understand how are they related with my problem. For compiling I use Visual C++ 2015 x64 Native Build Tools Command Prompt. I know that it's because of using const in function declaration. But array is not changing. So what's the deal?

Here is my code:

#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;
}

Program works well, I just want to understand why I get this warning.

I'm surprised it is only a warning. In C++ this would be entirely ill-formed.

Your function takes a const int* , which you then assign to an int* .

That is not const -correct.

I guess you meant const int *p ??

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM