A pointer to an array of strings...

I can't get the following code to work...

typedef struct
{
      char szItem[50];
}ShopItem;


static ShopItem Fruit[]=
{
      {"apples"},
      {"pears"},
      {"bananas"},
      {0}
};

int wList = FrmGetObjectIndex(pForm, 1001);
ListPtr pList = (ListPtr) FrmGetObjectPtr(pForm, wList);
      
LstSetListChoices(pList, Fruit, 3);

/*I get a compile error with the parameter "Fruit" on the
   last line of code*/

Any help would be appreciated.

RABMissouri





rabmissouriAsked:
Who is Participating?
 
msjammuConnect With a Mentor Commented:
This link conmtains very good information on this...
http://www.palmos.com/dev/support/docs/recipes/lists.html
0
 
bluelizardConnect With a Mentor Commented:
you can't use a regular array to set the list items. what you have to do is take the following steps:

1) pack all the items into one string, i.e., allocate memory and write your choices into it, so that it contains this data:
      apples[0]pears[0]bananas[0]
    ([0] meaning an ASCII-0 character)

2) call the function SysFormPointerArrayToStrings to build an array of pointers.  the paramters are the pointer to the above string and the number of items (3).  the result must be stored in a variable of the type MemHandle.

3) lock the memory chunk you got back from the function above (MemHandleLock(...)) and then call the function LstSetListChoices. paramters are the object pointer to the list (apparently, you already got that one right), the pointer that you got from MemHandeLock (just before), and again the number of items (3).  cast the pointer to the type Char **.

here's a sample (with your code added, i couldn't test it, so some typos might still be in there, but you should get the idea):

  typedef struct
  {
       char szItem[50];
  }ShopItem;
  static ShopItem Fruit[]=
  {
       {"apples"},
       {"pears"},
       {"bananas"},
       {0}
  };

  int wList = FrmGetObjectIndex(pForm, 1001);
  ListPtr pList = (ListPtr) FrmGetObjectPtr(pForm, wList);

  MemHandle st_listptrsmh;
  MemHandle st_listmh;
  Char *mp_temp;
  Char *mp_tempwork;
  Char *mp;
  UInt8 i;  
  st_listmh = MemHandleNew( 30 );
  if ( st_listmh != 0 )
  {
    mp_temp = MemHandleLock( st_listmh );
    mp_tempwork = mp_temp;
    for ( i=0; i<2; i++ )
    {
      StrPrintF( mp_tempwork, "%s", Fruit[i] );
      mp_tempwork = (Char *)( (UInt32)mp_tempwork + (UInt32)( StrLen ( mp_tempwork ) ) + 1 );
      // mp_tempwork now points to the byte after the zero-termination
    }
    MemHandleUnlock( st_listmh );
    st_listptrsmh = SysFormPointerArrayToStrings( mp_temp, 3 );
    if ( st_listptrsmh != NULL )
    {
      mp = MemHandleLock( st_listptrsmh );
      LstSetListChoices( plist, (Char **)mp, 3 );
      MemHandleUnlock ( st_listptrsmh );
    }
  }


--bluelizard
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.