Go Down

Topic: Converting a String to char * (Read 2762 times) previous topic - next topic

sydkahn

My goal is to print a string variable with a library supplied with an LCD interface board
Code: [Select]

String g="Test"
mydisp.println(g);

This throws and error on the mydisp. line while compiling :
pingpair_sleepy.cpp: In function 'void lcdsetup()':
pingpair_sleepy:286: error: no matching function for call to 'DigoleSerialDisp_I2C::println(String&)'
C:\Users\Syd\Downloads\arduino-1.0.1-windows\arduino-1.0.1\libraries\DigoleSerialDisp/DigoleSerialDisp.h:36: note: candidates are: void DigoleSerialDisp_I2C::println(char*)

how do i reference the string variable g so that println() will display it on the lcd?

My best guess so far is - but it doesn't work - it compiles - but does not display
Code: [Select]

  String  * p;
  String rr="TTTest";
  p=&rr;
  mydisp.println((char *)p);

The library imports the wire library
The library CPP for println()
Code: [Select]

void  DigoleSerialDisp_I2C::println(char *ch)
{
_myWire->beginTransmission(_I2Caddress);
  _myWire->write(ch);
  _myWire->write(13);
_myWire->endTransmission();
}


I have spent a couple of hours going through C++ tutorials and trying various things to no avail, I defer to the gurus for help, please?

Arrch

Best option:

Don't use the String object to begin with, use a string (null-terminated char array).

Working option:

http://arduino.cc/en/Reference/StringToCharArray

WizenedEE

Code: [Select]

char buf[10]; //make this the size of the String
g.toCharArray(buf, 10);


String is something defined by arduino, so you won't find it in a normal c++ book/website

Delta_G

Code: [Select]
char* p = "Test";

mydisp.println(p);


You don't need to String class at all.  It's not a good thing to use on microcontrollers anyway.

PaulS

Quote
My best guess so far is - but it doesn't work - it compiles - but does not display

Not even close. Casts only work between related types. You can not cast a String to a char array or char pointer.

A String is a class that wraps a char array. You have to get the char array from the String, to give to the LCD.

Look at the String::toCharArray() method.

dkl65

@WizenedEE: perhaps you need an extra space in the char array (11, not 10) to store the '\0' terninating character?

Why did the developers of Arduino create the String class when it is being strongly discouraged?

String::toCharArray() and String::getBytes() is the same thing.
Visit http://www.glenof.webs.com/!

Arrch


@WizenedEE: perhaps you need an extra space in the char array (11, not 10) to store the '\0' terninating character?

Why did the developers of Arduino create the String class when it is being strongly discouraged?

String::toCharArray() and String::getBytes() is the same thing.


Same reason for having delay(), some people need the crutch when they are just starting out.

zoomkat

Quote
Why did the developers of Arduino create the String class when it is being strongly discouraged?


Do note that the posters that are discouraging the string class apparently can't modify the OP's origional code to support their method. String class useage debate is more likely a red herring diverting from the problem. My question to the OP would be have you actually gotten anything to display on the LCD?
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

sydkahn

From the OP:  Yes - I used the suggestion char *  p = "Test"; and am using that in a function call to print a string at a particular location on the LCD;  I just pass in p and it works swimingly.

Being a C#/Java programmer as an avocation, string is the first thing I think of when a bunch of ascii needs displayed - I have to adjust my thinking now that I have seen how easy it is to pass around pointers.

When I start using the serial port in this project I will be using the buf[] version since I have no control over what might be coming in.

GoForSmoke

Remember you can keep string constants in flash memory and use pointers to access those. It allows bigger menus and verbose reporting.



Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

wanderson


Quote
Why did the developers of Arduino create the String class when it is being strongly discouraged?


Do note that the posters that are discouraging the string class apparently can't modify the OP's origional code to support their method. String class useage debate is more likely a red herring diverting from the problem. My question to the OP would be have you actually gotten anything to display on the LCD?


I suspect that the folks discouraging the use of the Arduino string class are doing so because of the well documented memory leak/fragmentation problems.
New true random number library available at: http://code.google.com/p/avr-hardware-random-number-generation/

Current version 1.0.1

GoForSmoke

What fits well on PC's don't always fit well on MCU's.

Why teach bad MCU habits when C strings are simple and give a complete set of functions that don't play with the heap or stack "for you". Learn pointers and ask why you ever used String Objects any way.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

zoomkat

Quote
I suspect that the folks discouraging the use of the Arduino string class are doing so because of the well documented memory leak/fragmentation problems.


But that probably has nothing to do with the current problem. If they can fix the OP's code with their solution, then they should. If their solution doesn't fix the problem, then they are just overheating their BS grinders on a non issue. Just be glad they don't diagnose and repair problems on aircraft that your family flies on.    ;)
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

zoomkat

Quote
When I start using the serial port in this project I will be using the buf[] version since I have no control over what might be coming in


You only show a piece of your code. If you are going to use a serial input for the text to display, have you made a simple serial test setup?
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Go Up