Converting String to char* error

Hi, i am making little big (not so big) project and i have a problem. In my library i have a function lcd_print which i am using to printing text or whatever i want to lcd. It look like that.

void lcd_print(char* text){
lcd.printstr(text);
}

In my main program, I am trying to use this function and as a parameter i am sending String or double. For example

String signCounter = '"Blabla";
lcd_print(signCounter);

But i got typical error:

  • error: cannot convert 'String' to 'char*' for argument '1' to 'void lcd_print(char*)'
    lcd_print(znakCasovac);

So i tried to use it like this:
lcd_print((String)signCounter);
but it didnt work.

I cant just use function lcd.print(), bcs i have declared lcd in library with function
void lcd_print(char* text) and i cant double it.

I tried everything I could think of, but it didnt work like i want.
I hope u can help me and that u understood what i mean with all it :smiley:

Friendly note: please use the code formatting in your post, it will make it so much easier to read your code and increase the chance that someone will help you.

to convert string to char string in c++ try this:
lcd_print(signCounter.c_str())

https://www.cplusplus.com/reference/string/string/c_str/

What does the lcd.printstr() function look like? Commonly you would overload the lcd_print function to accept String in addition to char*, but that would require that lcd.printstr() also accept String. Additionally, it is a good idea to have the char* declared const, in case you want to give the function a text literal.

void lcd_print(String text){
  lcd.printstr(text);
}

void lcd_print(const char* text){
  lcd.printstr(text);
}

You can also avoid the String entirely:

char signCounter[] = "Blabla";

Ty, this helped me, but i still have problem with double to char* and float to char* error.

For clarification:
I am doing alarm clock and i am printing time and date to lcd. I am using DHT library and print look like this.

lcd_print(DHT.temperature);

for float i am printing stopwatch

actualTime = (millis() - startTime) / 1000.0;
lcd_print(actualTime);

Which library are you using for the LCD?

< edit >
Most libraries would inherit from the print class, so you can just use lcd.print() for any type variable. Not sure what the lcd.printstr() function is intended to do that cannot be done with lcd.print(), or why you are going through your own lcd_print() function.

I am using LiquidCrystal_I2C library.

This is my library called lcd_wrapper

#include <Arduino.h>
#include <LiquidCrystal_I2C.h>
#include "lcd_wrapper.h"
LiquidCrystal_I2C lcd(0x27,16,2);

void lcd_init()
{
lcd.init();
lcd.backlight();
}

void lcd_clear()
{
lcd.clear();
}

void lcd_set_cursor(int y, int x)
{
lcd.setCursor(y, x);
}

void lcd_print(char text){*
lcd.printstr(text);
}

void lcd_print_at(int y, int x, char text){*
lcd_set_cursor(y, x);
lcd_print(text);
}

and this is a part of main code. I have there more functions, but i just post 1

#include "alarm_clock.h"
#include "lcd_wrapper.h"

void temp_hum()
{
int readDHT = DHT.read11(dht_apin);

lcd_set_cursor(0,0);
lcd_print("Temp: ");
lcd.print(DHT.temperature);
lcd_print("C");
lcd_set_cursor(0,1);
lcd_print("Hum: ");
lcd.print(DHT.humidity);
lcd_print("%");

delay(1000);
lcd_clear();
}

Yes i could just remove my void lcd_print(char text)* and use lcd.print from lcd library, but i got error, that in my main program lcd is not declared and idk why. My lcd is declared in library bcs of this function.

Welcome,

So it's a "XY problem", your real problem is that you don't know how to make lcd available in your main sketch.

What is the purpose of your lcd_wrapper library ? It looks like you want to extend the LiquidCrystal_I2C class, so I suggest you learn about class inheritance :wink:

Not exactly what i wanted but never mind.

I already fix it. Ty all for help

Glad you have solved your problem. However, this forum is not only for us to ask for help, it is a reference for anyone who may have the same problem later.

Therefore, even if you have solved an issue on your own, you should explain your solution in the thread so that other people can learn from it.

It is your function you can overload it for different types even to take string. For the future use const char* to pass strings so you could pass literals as well.

Ok, my bad, sorry :slight_smile:

I specify my problem again for better understanding.

I made my own library where i made my own methods for working with lcd. But if i wanted to print something with lcd.print i could not use it. So i had to made something like setter method.

void lcd_print(char text){*
lcd.print(text);
}

I wanted to use this, bcs this method i got in my last project (it is school project). Command lcd.print() can print int, float, string, double etc. But there i had as a paramether char, so i made close same methods for int, double, string.

void lcd_print_float(float text){
lcd.print(text);
}

void lcd_print_double(double text){
lcd.print(text);
}

void lcd_print_string(String text){
lcd.print(text);
}

It is not the best solution but it works. Yes i could do it easy and just use LiquidCrystal_I2C library. in my main program, but i wanted to separate them for better code look.

If I could publish the whole project here, it would be better.
Again, thanks all for help and patience :slight_smile:

What is stopping you ?

I thought this was a typo at first but you brought this again, so what is the asterisk * for?

Just mistake in formatting. Sorry

If i wanted to upload file i got an error, that i cant do it, bcs i am new here. I could do it through gitHub, but i forgot about that way :smiley:

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