"Incompatible types in assignment" error

Hi @all,

I am a newby, I have just bought the Arduino board, but I am already fascinated!
Because I do not know C/ C++ very well, I have a question about the following code.
Why does it not work? The Compiler always says:

In function ‘void setup()’:
error: incompatible types in assignment of ‘void’ to ‘char [20]’ In function ‘void makeString(int)’:

#include <LiquidCrystal.h>

LiquidCrystal lcd(49, 47, 45, 43, 41, 39, 37, 35, 33, 31, 29);

char printTemp[20] = "";
int mode = 1;
char buffer[20] = "";

void setup() {

  lcd.clear();

  buffer = makeString(mode);

  lcdPrint("Variable: ",buffer1);

}


// Subroutine: makeString  
void makeString(int zahl) {  
  char bufferMakeString[20];
  itoa (zahl,bufferMakeString,10);
  return(bufferMakeString);
}


// Subroutine: LCD PRINT  
void lcdPrint(char textLine1[20], char textLine2[20]) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(textLine1);
  lcd.setCursor(0, 1);
  lcd.print(textLine2);
}

void loop(){
}

What did I make wrong?
To be honest, I have tremendous problems with the variable types in C++!

Thank you very much for your help!!

Sebastian

You're trying to return something in a function that's defined as a void function.

Try this:

char* makeString(int zahl) {  
  char bufferMakeString[20];
  itoa (zahl,bufferMakeString,10);
  return bufferMakeString;
}

It will compile. But it will not work. This is because by the time the bufferMakeString returns to your code that calls it, the variable is no longer active in RAM. You'll need to use a global buffer variable for this.

Hi AlphaBeta,

thank you for your quick reply! Now there is the following error:

In function 'void setup()': error: incompatible types in assignment of 'char*' to 'char [20]

[UNTESTED CODE]

char bufferMakeString[20];

void setup(){ Serial.begin(9600); char* string = makeString(233445); Serial.println(string); }

void loop(){/no loop/}

char* makeString(int zahl) { itoa(zahl,bufferMakeString,10); return bufferMakeString; }

Yeah! Now it works! The LCD display shows the right number.
Here is the actual code:

#include <LiquidCrystal.h>

LiquidCrystal lcd(49, 47, 45, 43, 41, 39, 37, 35, 33, 31, 29);

char bufferMakeString[20];
int mode = 1;

void setup() {
  lcd.clear();

  char* string = makeString(mode);
  lcdPrint("Variable: ",string);
}

// Subroutine: makeString
char* makeString(int zahl) {
  itoa(zahl,bufferMakeString,10);
  return bufferMakeString;
} 

// Subroutine: LCD PRINT  
void lcdPrint(char textLine1[20], char textLine2[20]) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(textLine1);
  lcd.setCursor(0, 1);
  lcd.print(textLine2);
}

void loop(){
}

Another question:
What happens, if I want to call makeString in the loop?
May I always use

char* string = makeString(mode);

for this?

Or is this an illegal double definition of the variable?

It's perfectly legal, but you might as well use bufferMakeString.

Now an other question:

#include <LiquidCrystal.h>

LiquidCrystal lcd(49, 47, 45, 43, 41, 39, 37, 35, 33, 31, 29);

char makedString[20];
int userSelectionMode = 1;

void setup() {
  lcd.clear();
  char* stringToBeJoined = makeString(userSelectionMode);

  char* lcdOutput = join3Strings("Mode ", stringToBeJoined, " selected");
  lcdPrint(lcdOutput,"");
}

// Subroutine: makeString
char* makeString(int zahl) {
  itoa(zahl,makedString,10);
  return makedString;
} 

// Subroutine: join2Strings
char* join2Strings(char string1[40], char string2[40]) {
  strcat (string1, string2);
  return string1;
} 

// Subroutine: join3Strings
char* join3Strings(char string1[40], char string2[40], char string3[40]) {
  strcat (string1, string2);
  strcat (string1, string3);
  return string1;
} 

// Subroutine: LCD PRINT  
void lcdPrint(char textLine1[20], char textLine2[20]) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(textLine1);
  lcd.setCursor(0, 1);
  lcd.print(textLine2);
}

void loop(){
}

The important thing seems to be in “Subroutine: join3Strings”.
The result on the Display is “Mode 1”, not “Mode 1 selected”, as expected.
Is there a logical problem in there?
I just wanted to add these strings…

By the way: I think, the "join3String" function does work. If you call it with " char* lcdOutput = join3Strings("Mode ", "1", " selected");" it works properly.