I get an error saying:invalid conversion from 'int' to 'int*' [-fpermissive] when i try to enter an integer array as an argument in a function

When i run the code i get an error saying:
invalid conversion from 'int' to 'int*' [-fpermissive]
and i also get:
note: initializing argument 1 of 'void sendCm(int*)'
void sendCm(int cm[8])

this happens when the function sendCm runs.The function is supposed to send 8 bits to an lcd that is running in 4-bit mode.
Can someone please help?
I am still learning so..... yeah

const int d[4]={2,3,4,5};
const int cleards[8]={1,0,0,0,0,0,0,0};
const int rethome[8]={0,1,0,0,0,0,0,0};
const int dispcur[8]={1,1,1,1,0,0,0,0};
const int leftcur[8]={0,1,1,0,0,0,0,0};

void sendCm(int cm[8])
{
  int i=0;
  for(i=0;i<4;i++)
  {
  if(cm[i]==1)
   {
    digitalWrite(d[i],HIGH);
   }
   else
   {
    digitalWrite(d[i],LOW);
   }
  }
  digitalWrite(e,HIGH);
  digitalWrite(e,LOW);
 
    for(i=0;i<4;i++)
  {
  if(cm[i+4]==1)
   {
    digitalWrite(d[i],HIGH);
   }
   else if(cm[i+4]==0)
   {
    digitalWrite(d[i],LOW);
   }
  }
  digitalWrite(e,HIGH);
  digitalWrite(e,LOW);
}
void setup() {
  iopins();
  Serial.begin(9600);
  pinMode(i,INPUT);
  pinMode(rs,OUTPUT);
  pinMode(rw,OUTPUT);
  pinMode(e,OUTPUT);
  digitalWrite(rs,LOW);
  digitalWrite(rw,LOW);
  sendCm(cleards[8]);
  sendCm(rethome[8]);
  sendCm(dispcur[8]);
}

You are trying to pass 9th element of 8 element arrays

https://www.google.co.uk/search?q=how+to+pass+array+to+function+in+cpp&ie=UTF-8&oe=UTF-8&hl=en-gb&client=safari#sbfbu=1&pi=how%20to%20pass%20array%20to%20function%20in%20cpp

Welcome

Call like this

sendCm(cleards);

But, here is a better solution, using 16 times less memory :

const uint8_t cleards = 0b00000001; // note the bits are reversed compared to your arrays
const uint8_t rethome = 0b00000010;

void sendCm( uint8_t bits )
{
	for ( uint8_t i = 0; i < 2; i++ )
	{
		for( uint8_t j = 0; j < 4; j++ )
		{
			digitalWrite( d[j], bits & 1 ? HIGH : LOW );
			bits >>= 1;
		}
		digitalWrite( e, HIGH );
		digitalWrite( e, LOW );
	}
}
1 Like

Here's a similar thread with exactly the same problem as the one you're facing:

In short, change your code to:

void sendCm(const int (&cm)[8]) {
  // ...
}

void setup() {
  // ...
  sendCm(cleards);
  sendCm(rethome);
  sendCm(dispcur);
}

But the point raised by guix remains valid, of course, it's a bit wasteful of memory to store a zero or one in a 16-bit integer.

1 Like

Thanks!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.