Pages: [1]   Go Down
Author Topic: char msg[] versus const char text[]  (Read 1533 times)
0 Members and 1 Guest are viewing this topic.
Coppell, Texas
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm using an app that displays messages on a SURE 3208 8x8 Led Matrix.  It uses
the HT1632.h library routines.  One of the routines (HT1632.drawText) will
display a single character passed to it as a const char.  The specific parameter
is "const char text[]".

I would like to be able to take a string like char msg[] = {"message"}; and
iterate thru the string passing a single char to the HT1632.drawText function.

When I call the function with msg
  • as the const char text[] paramerter, the
compiler complains that I can't convert char to const char.  I've been unable
to find the right code that would allow me to iterate thru each char in
msg[] and pass this char to the HT1632.drawText function.  

Anyone have an ideas how this might be done?   Following is a code snipette..
I would like to be able to write a function that takes a char msg[] string
and breaks it down into single char that can be passed to the HT1632.drawText
function...
  
Code:
void loop()
  {
    char msg1[7] =  {'T','U','N','E',' ',' '};
    char msg2[7] =  {'F','M',' ',' ',' ',' '};
    char msg3[7] =  {'1','0','7','.','3',' '};
    int  xstart;
    int  y = 1;
 
    //message 1
    HT1632.clear();
    HT1632.render();
    xstart = 4;
    for(int z = 0, x = xstart; z < 6; z++, x+=6)
    {
      char val[1] = {msg1[z]};
      HT1632.drawText(val, x, y, FONT_5X7, FONT_5X7_W, FONT_5X7_H, FONT_5X7_STEP);
    }
    HT1632.render();
    delay(1000);
  }

Moderator: CODE TAGS
« Last Edit: February 22, 2012, 05:56:17 am by AWOL » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26487
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
      HT1632.drawText(msg[z], x, y, FONT_5X7, FONT_5X7_W, FONT_5X7_H, FONT_5X7_STEP);
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

India
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
OS Developer, ASIC and FPGA designer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Function prototype is :
Code:
void drawText(const char [], int x, int y, const char font [], const char font_width [], char font_height, int font_glyph_step, char gutter_space = 1);

HT1632.drawText(val, x, y, FONT_5X7, FONT_5X7_W, FONT_5X7_H, FONT_5X7_STEP);
I have Tested (val, x, y,------------> do not have syntax error
can you check the types or constants in header files HT1632.h
 FONT_5X7, FONT_5X7_W, FONT_5X7_H, FONT_5X7_STEP

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 633
Posts: 50189
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Anyone have an ideas how this might be done?
With a cast. What the compiler is telling you is that you have an array that is not const, where the function is expecting that the array is const. So, lie to the function:
Code:
      HT1632.drawText((const char *)msg, x, y, FONT_5X7, FONT_5X7_W, FONT_5X7_H, FONT_5X7_STEP);
Tell it that your non-const array really is const.
Logged

Coppell, Texas
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your right on Paul.... although while it does compile witout complaing...
the result is not as expected.  What I would expect to see is "TUNE"
on the Matrix, instead it seems to be a series of random chars... ??

Code:
    //message 1
    HT1632.clear();
    HT1632.render();
    xstart = 4;
    char msg[] = {"TUNE  "};
    for(int z = 0, x = xstart; z < 6; z++, x+=6)
    {
      HT1632.drawText((const char *)msg[z], x, y, FONT_5X7, FONT_5X7_WIDTH, FONT_5X7_HEIGHT, FONT_5X7_STEP_GLYPH);
    }
    HT1632.render();
    delay(1000);
Logged

India
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
OS Developer, ASIC and FPGA designer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
HT1632.drawText((const char *)msg[z], x, y, FONT_5X7, FONT_5X7_WIDTH, FONT_5X7_HEIGHT, FONT_5X7_STEP_GLYPH);
  //msg[z] <--------------------- Is  hold a Value not the address
 //(const char *)msg[z]  <--------------- you are converting  a value to address
// (const char *)&msg[z] <--------------- correct syntax you are converting the address to a   (const char *)
Logged

Coppell, Texas
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

SOLVED!  Thank you.  Those nasty pointers... one area of C I always tried to avoid.
Thank you all..
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 633
Posts: 50189
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Those nasty pointers... one area of C I always tried to avoid.
You'll continue, unfortunately, to have problems until you learn to embrace them.

It appears that the HT1632 instance expects to draw a string of text, not just one character. So, it isn't obvious why you need to take such great pains to call it many times, for one letter at a time.
Logged

Austin, TX
Offline Offline
Full Member
***
Karma: 0
Posts: 134
I make my own electricity.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Those nasty pointers... one area of C I always tried to avoid.
You'll continue, unfortunately, to have problems until you learn to embrace them.

True, that.

Functions declare their formal pointer parameters as "const" when they are not going to write to the referenced memory.  It helps programmers know that they can declare the actual value as a "const" so the compiler and/or loader can put the value into read-only memory.  It also helps catch the opposite problem -- passing in a "const" when the function is actually going to try and write to the memory.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 633
Posts: 50189
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Functions declare their formal pointer parameters as "const" when they are not going to write to the referenced memory.
What I don't understand about the whole const thing is that if a function declares an array argument const, it is signing a contract to not modify that variable. So, why does passing the function an non-const array cause problems?

If the function declares that it accepts an array, and does not mark it as const, then passing a const array to it should generate a warning or an error. But, the other should not, I wouldn't think.
Logged

Austin, TX
Offline Offline
Full Member
***
Karma: 0
Posts: 134
I make my own electricity.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Functions declare their formal pointer parameters as "const" when they are not going to write to the referenced memory.
What I don't understand about the whole const thing is that if a function declares an array argument const, it is signing a contract to not modify that variable. So, why does passing the function an non-const array cause problems?

If the function declares that it accepts an array, and does not mark it as const, then passing a const array to it should generate a warning or an error. But, the other should not, I wouldn't think.

I don't believe passing in a (char *) when the formal parameter is a (const char *) is an error -- neither of this throws an error in a sketch I'm working on --

Code:
        if (strncmp((char *) buffer, "sBox", 4) == 0)

or

Code:
        if (strncmp((const char *) buffer, "sBox", 4) == 0)

"buffer" is declared as a (unsigned char[66]), because some of the data are unsigned bytes and the only thing that causes an error or warning is what I remove the cast completely.  Then I get

Code:
dBox_rtu_SPI:334: error: invalid conversion from 'unsigned char*' to 'const char*'
dBox_rtu_SPI:334: error: initializing argument 1 of 'int strncmp(const char*, const char*, size_t)'
Logged

Pages: [1]   Go Up
Jump to: