Home:ALL Converter>Array in Progmem wrong subscript?

Array in Progmem wrong subscript?

Ask Time:2013-09-26T04:28:18         Author:user2371490

Json Formatter

I keep having trouble with getting this code to work properly. My goal is to show strings which are placed in PROGMEM on a LCD. The array with pointers to these strings is also in PROGMEM. The function is called with a variable which in turn is translated to an index number for reading out the array. Ofcourse pgmspace.h is included in the code.

The errors I keep getting are: array subscript has type 'char' [-Wchar-subscripts] - initialization makes pointer from integer without a cast [enabled by default]

Could someone point out what i'm missing here?

Working on code for AVR GCC, my IDE is Eclipse.

    const char wf0[] PROGMEM= "OFF ";
    const char wf1[] PROGMEM= "SIN ";
    const char wf2[] PROGMEM= "TRI ";
    const char wf3[] PROGMEM= "S+T ";
    const char wf4[] PROGMEM= "PUL ";
    const char wf5[] PROGMEM= "P+S ";
    const char wf6[] PROGMEM= "P+T ";
    const char wf7[] PROGMEM= "P+ST";
    const char wf8[] PROGMEM= "NOI ";
    const char wf9[] PROGMEM= "N+S ";
    const char wf10[] PROGMEM= "N+T ";
    const char wf11[] PROGMEM= "NST ";
    const char wf12[] PROGMEM= "N+P ";
    const char wf13[] PROGMEM= "NPS ";
    const char wf14[] PROGMEM= "NPT ";
    const char wf15[] PROGMEM= "NPTS";

    const char * const arrayWaveform[] PROGMEM= {wf0,wf1,wf2,wf3,wf4,wf5,wf6,wf7,wf8,wf9,wf10,wf11,wf12,wf13,wf14,wf15};

...

void showWaveform (char ctrlValue)
{
    char hex = (ctrlValue & 0xf0)>>4;

    char tempText[4];
    char* data = pgm_read_byte(&arrayWaveform[hex]); // <<shows up both errors here
    strcpy_P (tempText, data);
    for (char x=0;x<4;x++)
        {
        char2LCD(tempText[x]);  // <<shows up error: array subscript has type 'char'
        }
}

Author:user2371490,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/19014592/array-in-progmem-wrong-subscript
CAFxX :

This should do:\n\nvoid showWaveform (char ctrlValue)\n{\n char hex = (ctrlValue & 0xf0)>>4;\n char *pstr, tempText[5];\n memcpy_P(&pstr, arrayWaveform+hex, sizeof(char*));\n strncpy_P(tempText, pstr, sizeof(tempText));\n for (int x=0; x<4 && tempText[x] != 0; x++)\n {\n char2LCD(tempText[x]); \n }\n}\n\n\nThere were multiple issues with your code. First and foremost arrayWaveform itself is stored in PROGMEM, so you can't simply access it using square brackets. Second, tempText must be at least 5 elements long (otherwise strcpy_P can write past the end of the array).\n\nYou may want to carefully read the docs about PROGMEM, to fully understand what's going on. Please note that if you have a recent copy of AVR-GCC there is also the new __flash syntax that allows to get rid of all the pgm_read_* and *_P machinery. I have never tested it, so YMMV.",
2013-09-25T20:42:58
P0W :

In tempText[x] and arrayWaveform[hex] the index is of type signed char which can take up negavtive values too\n\nChange it to unsigned char\n\nSee GCC Warnings :\n\n\n Wchar-subscripts Warn if an array subscript has type char. This is a\n common cause of error, as programmers often forget that this type is\n signed on some machines. This warning is enabled by -Wall.\n",
2013-09-25T20:36:44
yy