Compilation warning: invalid conversion

I want to pass both integer arrays and constant integer arrays as parameters to the same function. This is exemplified by this code:

const int CIA[] = {1,2,3,4};
int ia[] = {11,12,13,14};

void setup() {
  // put your setup code here, to run once:

void loop() {
  // put your main code here, to run repeatedly:


void func(int a[]) {

When compiling I get this warning:

testPtr.ino:8:11: warning: invalid conversion from 'const int*' to 'int*'

The program works ok, so it is more a matter of learning what the difference is between a constant integer array and a normal integer array when passing them as parameters.

I understand that at runtime the passing is done with pointers, but I don't see the difference between the two types of arrays in this case. I have browsed a plentyful of threads regarding similar problems, but none of them 'fits' my special problem.

Most grateful for being enlightened :wink:
I have no experience in C or C++.


Does the function modify the array?

If not then you have two choices.

  1. Ignore the warning, it's not really hurting anything. You know that it is safe to cast an int to a const int calling a function so you say, "Thanks for the warning, duly noted, but I'm not doing anything dangerous." and you move on to the real errors.

  2. You rewrite the function to take const int as the parameter type and go through the code and anywhere you find yourself calling the function with an int you simply cast that to const int and call the function with it. This doesn't change what's really happening at all, but it will make the warning go away since you are explicitly casing the int to const int now and that is perfectly legal.

You know that it is safe to cast an int to a const int

--> that's the opposite and it's not safe...

You are missing a Serial.begin(115200); in the setup()

I don't see that warning in the 1.8.0 version on my Mac but it's not a bad warning.

What it tells you is that you define CIA as an array of integers constant (read only) --> so you are telling the compiler that nothing can change those integers. Then later on you call func(CIA); and the function is defined as this:

void func([color=red]int a[ ])[/color] {
  Serial.println(a[ 2 ]);

so in the function the array could theoretically be used to change a values in the array. The compiler notices that and lets you know.

Oh, I see. I had it backwards. I thought the function took const int and he was passing in an int. That’s what I was saying was safe. It’s ok to promise not to change something when you can. Passing a const int to a function that takes an int as an argument is definitely not. It’s not ok to take something you’re not supposed to change and not promise not to change it.

Changing the function to take a const argument gets rid of the warning:

void func(const int a[]) {

This also has the advantage of generating an error if something in the function tries to modify the array. Ignoring the warning would allow your 'const' array to get overwritten if the function had such an error (which it doesn't).

So it was that easy :wink: Thanks a plenty :slight_smile: