Pages: [1]   Go Down
Author Topic: Converting a String to char *  (Read 1900 times)
0 Members and 1 Guest are viewing this topic.
Gettysburg, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My goal is to print a string variable with a library supplied with an LCD interface board
Code:
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:
  String  * p;
  String rr="TTTest";
  p=&rr;
  mydisp.println((char *)p);
The library imports the wire library
The library CPP for println()
Code:
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?
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
Logged

Offline Offline
God Member
*****
Karma: 10
Posts: 651
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8966
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Gettysburg, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4036
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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



Logged

Examples can be found in your IDE.

Dallas, Texas
Offline Offline
God Member
*****
Karma: 3
Posts: 719
Old, decrepit curmugeon
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

New true random number library available at: http://code.google.com/p/avr-hardware-random-number-generation/

Current version 0.7.2

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4036
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Examples can be found in your IDE.

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8966
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.    smiley-wink
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8966
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: