This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

pointer to array is killing me!!!

I'm having serious problem with the following code:

unsigned char code * Progtxtstring_ENG[] =
{
  "PROGRAM",
  "SYSTEM"
};
unsigned char code * pMenu;
//User selected English
pMenu = Progtxtstring_ENG;

//Why doesn't this work, it just get garbage out:
printf("%s",pMenu[0]);
// This line below works
printf("%s",Progtxtstring_SWE[0])
I'm converting my program to handle selection of multiple languages. I want to select what language to print by a integer.
But here I'm trying to point to the correct "pointerarray" that contains the language. (Progtxtstring_XXX for language XXX) But I can't get it right. I'm guessing I am messing with the data types. It's an Philips 80C552 with the latest compiler. Would really appritiate some help.

  • Progtxtstring_ENG is an array of pointers; pMenu is just a single pointer.

    What do you need to do if you want to extract a single element from an array...?

  • I want to use the pMenu just as I use the Progtxtstring_ENG. I want to switch between arrays of pointers so I can point to different language.
    Maybe this will explain better:

    unsigned char code * Progtxtstring_SWE[] =
    {
      "Program",
      "Tank tryck"
    };
    unsigned char code * Progtxtstring_ENG[] =
    {
      "Program",
      "Tank Pressure"
    };
    //I want an pointer to the array of pointers
    unsigned char code * pMenu;
    //User selected Swedish. (Not shown)
    pMenu = Progtxtstring_SWE;
    
    // Here I'm trying to print "Tank Tryck"
    printf("%s",pMenu[1]);
    
    // This line Prints "Tank Tryck"
    printf("%s",Progtxtstring_SWE[1])
    

    Guess Andy is trying to guide me in the right direction, but I'm to confused right now. I know I'm messing up with pointers to array of pointers but I can't see clearly now. Please give me some examples

  • Hmmmm
    I guess I need an char **ptr to access the array of pointers. It's clearing up a bit...

  • perhaps re-writing the definitions very slightly might help:

    char    *pChar
    might make it easier for you to see that *pChar is a char, and
    char*    pChar
    might make it easier for you to see that pChar is a char*; ie, a pointer to a char

  • "I want to use the pMenu just as I use the Progtxtstring_ENG. I want to switch between arrays of pointers so I can point to different language."

    Why confuse yourself with an extra pointer, then?
    Why not just use a 2-dimensional array?

  • Why not just use a 2-dimensional array?

    Because true 2-dimensional arrays essentially don't exist in C? You can define arrays-of-arrays, sure, by you can't do very much with them that wouldn't silently turn them into a pointer-to-array.

    Or because he would need a dynamic array for this to work, with more than one unknown dimensions, found in some other place than the outer-most indexing level:

    char string_table[n_tables][/*unknown*/][/*unknown*/]

    which of course C doesn't support at all. The structure of those arrays shown here is actually quite sensible. It's just his pointer pMenu that was of the wrong type.

  • "Or because he would need a dynamic array for this to work"

    Really?
    I'd assumed there'd be a fixed number of languages, and a fixed number of entries in each menu - especially as he's storing them in code space:

    char menu[NUM_LANGUAGES][NUM_ENTRIES]

  • "Because true 2-dimensional arrays essentially don't exist in C? You can define arrays-of-arrays, sure, by you can't do very much with them that wouldn't silently turn them into a pointer-to-array."

    But, surely, the same could be said of 1-dimensional arrays - you can't do much with them, either, that doesn't silently turn them into pointers?
    After all, the offset operator [] can be applied to any pointer - whether or not it was defined as an array!