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

Back to Subject List

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

Read: 502 times
Silistra
Bulgaria


 
#187530 - about (tables of) pointers to const strings
Responding to: Stefan Kanev'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      Stefan KAnev      05/23/12 23:27      
   Does not compile      Maarten Brock      05/24/12 02:05      
      my question was      Stefan KAnev      05/24/12 04:21      
         Common to play with #define EXTERN      Per Westermark      05/24/12 05:41      
            Common doesn't mean recommended      Oliver Sedlacek      05/25/12 09:43      
               well I recommend it      Erik Malund      05/25/12 10:13      
               No - not hard to maintain at all      Per Westermark      05/25/12 12:44      
   C strings      Per Westermark      05/24/12 03:00      
   Whats wrong with a header file?        Oliver Sedlacek      05/24/12 10:08      
      nothing wrong, double work      Stefan KAnev      05/24/12 11:04      
         It works, and has advantages      Oliver Sedlacek      05/25/12 07:06      
         I got in a total absolute magnificent mess      Erik Malund      05/25/12 07:58      
   thanks to everyone, and next question is      Stefan KAnev      05/25/12 14:46      
      about (tables of) pointers to const strings      Stefan KAnev      05/25/12 14:47      
         C++ and style      Maarten Brock      05/26/12 03:57      

Back to Subject List