Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/25/12 20:47
Modified:
  05/25/12 20:48

Read: times


 
#187530 - about (tables of) pointers to const strings
Responding to: ???'s previous message
i'm trying (learning C this way ), to define structures , usable in user input menu. Each structure will hold some data, which describes fields to be entered by user in submenu, and strings , related to these fields, belonging to submenu. First problem was to solve representation of strings, related to enumerated data type (and how to combine them with single prompt messages, related to numerical types) . In code below such type is declared as 'edittype_bool'. Code compiles in Dev-C (Win) and executes as i "want" .
My questions- what errors i have , and what is optimal solution for code block , designated as "B-BLOCK" ?

#include <cstdlib>
#include <cstdio>

#define  recordtype1  1 //not important here
#define  recordtype2  2 //not important here

#define edittype_bool 0
#define edittype_number 1
typedef struct typedescriptor
  {
  char TDrecordtype;
  char TDfieldscount;
  const char* TDfieldstypes;
  const void *TDmsgORptrtomsg;
  } TD;

//-------- user input submenu 2  description
     const char msg_TWO_a[]="How many beers per day  You drink?-";
     const char msg_TWO_b[]="How many coffee per day  You drink?-";
  const void *editmessages_td_two[]= //array of pointers
     { msg_TWO_a, //pointer to string
       msg_TWO_b  //pointer to string
     };
  const char fieldstype_td_two[]={edittype_number,edittype_number};
const TD TD_two=
 { recordtype2 ,//not important now
  2 , // 2 fields to input/change
  fieldstype_td_two,
  editmessages_td_two
 };
//-------- user input submenu 1  description


     const char msg_ONE_aA[]="Marriage status?-";
     const char msg_ONE_aB[]="Married.";
     const char msg_ONE_aC[]="Non married.";
    const void *msg_ONE_a_tab[]=
      { msg_ONE_aA,
        msg_ONE_aB,
        msg_ONE_aC
      };

    const char msg_ONE_b[]="How many pets You have?-";
    const char msg_ONE_c[]="How many friends You have?-";


 const void *editmessages_td_one[]=
   { msg_ONE_a_tab,//pointer to array of pointers
     msg_ONE_b,    //pointer to string
     msg_ONE_c     //pointer to string
   };

 const char fieldstype_td_one[]={edittype_bool,edittype_number,edittype_number};
const TD TD_one=
 { recordtype1 ,//not important now
  3 , // 3 fields to input/change
  fieldstype_td_one,
  editmessages_td_one
 };



//---- user input menu description
#define SUBMENUScount 2
const TD *EditMenu[SUBMENUScount]=
  {&TD_one,
   &TD_two
  };


//---- test routine for accessing strings
void testaccess(void)
 {char submenu,fieldnum,fieldscount,fieldtype;
  const char* fieldonestring;

  void **ptr1;
  //void **ptr2;
  void *ptr3;



 for (submenu=0;submenu<SUBMENUScount;submenu++)
  {printf("Submenu %d ---------n",submenu);
   fieldscount=EditMenu[submenu]->TDfieldscount;

   for (fieldnum=0;fieldnum<fieldscount;fieldnum++)
      {fieldtype=EditMenu[submenu]->TDfieldstypes[fieldnum];
       ptr1=(void**)EditMenu[submenu]->TDmsgORptrtomsg;

       if (fieldtype!=edittype_bool)
         {
          fieldonestring=(const char*)ptr1[fieldnum];
          printf("field %d ,prompt message= %s n",fieldnum,fieldonestring);
         } //edittype!=bool
         else
         {//B-BLOCK start
           ptr3=ptr1[fieldnum];
           ptr1=(void**)ptr3;

           fieldonestring=(const char*)ptr1[0];
           printf("field %d ,prompt message= %s n",fieldnum,  fieldonestring);

           fieldonestring=(const char*)ptr1[1];
           printf("--when 0,show as= %s n",  fieldonestring);

           fieldonestring=(const char*)ptr1[2];
           printf("--when 1,show as= %s n",  fieldonestring);
        //B-BLOCK end      
         };// edittype==bool
      };// end for(fieldnum)
  };//end for( submenu)
 } //end void testaccess()
//----------------------
int main(int argc, char *argv[])
{
  testaccess();
    system("PAUSE");
    return 0;//EXIT_SUCCESS;
}


 


Output is:
Press any key to continue . . . 
Submenu 0 ---------
field 0 ,prompt message= Marriage status?- 
--when 0,show as= Married. 
--when 1,show as= Non married. 
field 1 ,prompt message= How many pets You have?- 
field 2 ,prompt message= How many friends You have?- 
Submenu 1 ---------
field 0 ,prompt message= How many beers per day  You drink?- 
field 1 ,prompt message= How many coffee per day  You drink?- 


 

best regards

List of 15 messages in thread
TopicAuthorDate
const strings in C            01/01/70 00:00      
   Does not compile            01/01/70 00:00      
      my question was            01/01/70 00:00      
         Common to play with #define EXTERN            01/01/70 00:00      
            Common doesn't mean recommended            01/01/70 00:00      
               well I recommend it            01/01/70 00:00      
               No - not hard to maintain at all            01/01/70 00:00      
   C strings            01/01/70 00:00      
   Whats wrong with a header file?            01/01/70 00:00      
      nothing wrong, double work            01/01/70 00:00      
         It works, and has advantages            01/01/70 00:00      
         I got in a total absolute magnificent mess            01/01/70 00:00      
   thanks to everyone, and next question is            01/01/70 00:00      
      about (tables of) pointers to const strings            01/01/70 00:00      
         C++ and style            01/01/70 00:00      

Back to Subject List