Converting a String to char *

My goal is to print a string variable with a library supplied with an LCD interface board

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

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

The library imports the wire library
The library CPP for println()

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?

Best option:

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

Working option:

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

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.

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.

@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.

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.

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

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?

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.

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

zoomkat:

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.

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.

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. :wink:

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?

can any one help me regarding string problem?

String temp="This is ankush";

this is my temp string,i want to find out whether ankush is present in the string or not.how do i get tet to know?

strstr fun is used but for that string must be char array can not used in string datatype.

please help me

this is my temp string,i want to find out whether ankush is present in the string

You have a String, not a string.

You have access to the String class documentation. There are methods like indexOf() that might be useful in your memory wasting code.