LCD printing difficulties - const char

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi freaks...

So I am using Martin Thomas's LCD library for SED1520 based displays. Text is put on the display with the following function:

void lcd_text(uint8_t left, uint8_t top, uint8_t font, const char *str)
{
	// LCD operations go here
}

As you can see, this function must be passed a const char. I'm having difficulty coming up with an easy way to print strings that may change, such as ADC readings or the like. My gut feeling is that I will just have to write a version of this function that does not require const char. I'm open to any suggestions.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

const in this case indicates that str is not changed within the lcd_text() function. You are free to change it all you want outside the function. You just don't declare the string that you are passing in as const.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why not just cast the non-const string to be const when specifying the parm to lcd_text. That is:

{
	char mesg[20];

	strcpy(mesg, "Hello World");
	lcd_text(x, y, font, (const char *) mesg);
}

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wow.. stupid mistake on my part.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Why not just cast the non-const string to be const when specifying the parm to lcd_text.

No need to explicitly cast. The compiler will automatically cast it. Of course, there is no harm in explicitly casting either, and it might make the code clearer.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi wrote:
The compiler will automatically cast it. Of course, there is no harm in explicitly casting either, and it might make the code clearer.

Well it should also prevent a compiler warning :wink: (at least you'd hope the compiler would be issuing a warning about this!)

In fact casts are often the way to quell compiler warnings as long as you are happy with the use being made of a pointer and the compiler isn't actually warning about a valid mistake.

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

>> The compiler will automatically cast it.

> Well it should also prevent a compiler warning

No, there's no reason for a warning. Passing a
modifiable object to a function that is explicitly
declared to not change the object is always a safe
operation. The reverse case (losing qualification
of the object while passing it to a function) would
be worth a warning though.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.