Go Down

Topic: char msg[] versus const char text[] (Read 1 time) previous topic - next topic

miker

Feb 22, 2012, 11:53 am Last Edit: Feb 22, 2012, 11:56 am by AWOL Reason: 1
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: [Select]
    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

AWOL

Code: [Select]
      HT1632.drawText(msg[z], x, y, FONT_5X7, FONT_5X7_W, FONT_5X7_H, FONT_5X7_STEP);
"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.

pravas

Function prototype is :
Code: [Select]
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


PaulS

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: [Select]
      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.

miker

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: [Select]

    //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);

pravas

Code: [Select]

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 *)

miker

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

PaulS

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.

jfhaugh


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.

PaulS

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.

jfhaugh


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: [Select]

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


or

Code: [Select]

        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: [Select]
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)'

Go Up