Thread: HELP!! Urgent !!

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    30

    HELP!! Urgent !!

    Hi, can anyone help me ? i dunno how to solve this. Pls help.
    Please follow the step to reach the Question.
    Thanks a lot!!!

    Code:
    Step (1): Type structure definition
    
    typedef struct menu_item_struct
    {
    	  ros_data_hdr_t            data_hdr;       /* place holder for ros header */
    	  struct menu_item_struct   *next_item;     /* next menu item in the list */
    	  UINT8                     position;       /* menu item position */
    	   struct
    	  {
    	    uniChar_t               uniString[1];   /* place holder for string of Unicode characters */
    	    UINT8                   asciiString[1]; /* place holder for string of ASCII characters */
    	  } text;
    } menu_item_t;
    
    
    typedef struct
    {
    	  UINT8 row;                     /* start row for menu */
    	  UINT8 max_item;                /* largest menu item position */
    	  menu_item_t *first_menu_item;  /* pointer to the first menu item in the list */
    } menu_item_list_t;
    
    
    
    Step (2): Global variables declaration
    
    menu_item_t * * m_menuitem;
    UINT8 index=0;
    
    
    
    Step (3): 
    
    void setMenuItems( UINT8 index, menu_item_t * menu_item )
    {
    
    	m_menuitem[index] = menu_item;
    	
    }
    //First, assume that the above "setMenuItems" was called for 5 times. Therefore, m_menuitem[] should have 5 elements now, and index has been incremented to 5.
    
    
    
    Step (4):
    //After that, assume that "process_menuItemfunc()" is called. At this point, m_menuitem[]contains 5 items.
    
    void process_menuItemfunc()
    {
    
       //In this function, I have to call "putMenu(menuItem_t *menuItemPtr, uint8_t num_of_items)" as defined below.
       //Notice that the 1st parameter of the "putMenu()" is a pointer to an array of menu items to be displayed.
       //The menu item is of class "menuItem_t" as defined below.
       
       //So, before I could call "putMenu", I have to populate the 1st parameters. Because the "m_menuitem" and "menuItem_t" are of different types.
       //Therefore, I cannot pass in "m_menuitem" directly to "putMenu()" as its 1st parameter.
       
       //QUESTION : How to populate/construct the 1st parameter, which is the pointer to an array of menu items of class menuItem_t?
                    
       
    
    }
    
    
    
    //A class called menuItem_t is defined as below
    class menuItem_t {
    
    public:
            UINT8 position;
            UINT16 state;
            UINT16 *uniString;
      
    };
    
    
    //*menuItemPtr - This is a pointer to an array of menu items to be displayed. 
    void putMenu (menuItem_t *menuItemPtr, UINT8 num_of_items)
    {
       //some processing here
    }
    Last edited by huwan; 06-07-2007 at 06:04 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    From the announcements:
    3. Do not put URGENT!, or NEED HELP NOW, etc. in your title; it will not make people look at it any faster. Doing this makes many old time helpers on this board not look at the post at all.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Well he did have two exclamation marks instead of just one...

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Please follow the step to reach the Question.
    Doesn't one usually follow the steps of a question to reach an answer, not the other way around?

    Assuming that UINT8 is an integer type, probably something like this,
    Code:
    typedef unsigned char UINT8;
    Then this function cannot modify index:
    Code:
    void setMenuItems( UINT8 index, menu_item_t * menu_item )
    {
    
    	m_menuitem[index] = menu_item;
    	
    }
    //First, assume that the above "setMenuItems" was called for 5 times. Therefore, m_menuitem[] should have 5 elements now, and index has been incremented to 5.
    Which means that the calling function must modify index, which makes that function pretty much useless. Besides, index is global, isn't it?

    That function wouldn't even work unless the calling function also allocated some memory.

    Code:
    //A class called menuItem_t is defined as below
    class menuItem_t {
    
    public:
            UINT8 position;
            UINT16 state;
            UINT16 *uniString;
      
    };
    That might as well be a struct.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Yup. There were some mistakes n errors in the code.
    Actually I did not include the full code here that's why the code not complete and got some mistakes. Sorry about that.

    But put aside the mistakes first.

    For this "menuItem_t" class, it should be a structure instead of class.
    So, it would look like this:-
    Code:
    typedef struct
    {
    	  UINT8 position;
                      UINT16 state;
                      UINT16 *uniString;
    } menuItem_t;

    For "setMenuItems(...)", it should not taken "index" as a parameter.
    So, it should be like this:-
    Code:
    void setMenuItems( menu_item_t * menu_item )
    {
    
    	m_menuitem[index] = menu_item;
    	
    }
    Ok, if there is still mistakes or errors , then just put them aside first.

    What I actually want to know is, the above "setMenuItems" was called for 5 times. Therefore, m_menuitem[] should have 5 elements now, and index has been incremented to 5. Am I right?
    So, what actually contain inside the "m_menuitem[]" now are the 5 pointers to "menu_item_t". Am I right?

    I have "m_menuitem" now. You can see the 1st parameter from this function "putMenu (menuItem_t *menuItemPtr, UINT8 num_of_items)", it actually request a pointer to an array of type "menuItem_t, am I right?

    So, I need to construct or get this pointer to an array of type "menuItem_t" from "m_menuitem". My question is how to do so? I don't know how to construct it so that I could pass it to "putMenu(..)" for further processing.
    Please help.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What I actually want to know is, the above "setMenuItems" was called for 5 times. Therefore, m_menuitem[] should have 5 elements now, and index has been incremented to 5. Am I right?
    So, what actually contain inside the "m_menuitem[]" now are the 5 pointers to "menu_item_t". Am I right?
    Yes, assuming the calling function handled a few things properly, like making sure that enough memory was allocated, and index was incremented.

    I have "m_menuitem" now. You can see the 1st parameter from this function "putMenu (menuItem_t *menuItemPtr, UINT8 num_of_items)", it actually request a pointer to an array of type "menuItem_t, am I right?

    So, I need to construct or get this pointer to an array of type "menuItem_t" from "m_menuitem". My question is how to do so? I don't know how to construct it so that I could pass it to "putMenu(..)" for further processing.
    So basically your problem simplifies down to converting this
    Code:
    typedef struct menu_item_struct
    {
    	  ros_data_hdr_t            data_hdr;       /* place holder for ros header */
    	  struct menu_item_struct   *next_item;     /* next menu item in the list */
    	  UINT8                     position;       /* menu item position */
    	   struct
    	  {
    	    uniChar_t               uniString[1];   /* place holder for string of Unicode characters */
    	    UINT8                   asciiString[1]; /* place holder for string of ASCII characters */
    	  } text;
    } menu_item_t;
    
    menu_item_t * * m_menuitem;
    to this:
    Code:
    class menuItem_t {
    
    public:
            UINT8 position;
            UINT16 state;
            UINT16 *uniString;
      
    };
    
    
    //*menuItemPtr - This is a pointer to an array of menu items to be displayed. 
    void putMenu (menuItem_t *menuItemPtr, UINT8 num_of_items)
    Is that right?

    Well, you'll probably need a conversion function of some kind. Perhaps the best way to implement this would be to create a menuItem_t(menu_item_t&) constructor. Something like this.
    Code:
    class menuItem_t {
    public:
            UINT8 position;
            UINT16 state;
            UINT16 *uniString;
    
            menuItem_t(menu_item_t &other);
    };
    
    menuItem_t::menuItem_t(menu_item_t &other) {
        position = other.position;
    }
    I don't know how you'd create the state and uniString members of menuItem_t out of the members of menu_item_t, but however you do it, do it in that function, in the same place I set position.

    (To other C++ programmers: I didn't feel like explaining initializers.)

    Using constructors is very easy. You can basically do this:
    Code:
    menu_item_t from;
    menuItem_t to = from;
    // now you have to
    BTW, try to make your variable and type names different. If two variables are different but you pronounce them the same way (or nearly so), then consider renaming them.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Basically you got what I meant.
    But, menuItem_t is not a class anymore.
    It is a structure like this:-

    Code:
    typedef struct
    {
    	  UINT8 position;
                      UINT16 state;
                      UINT16 *uniString;
    } menuItem_t;
    Please help. Thanks a lot.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    In C++, you can have constructors in structs as well. But perhaps the best thing to do would be to have a conversion function.
    Code:
    menuItem_t convert(menu_item_t &other) {
        menuItem_t m;
        m.position = other.position;
        return m;
    }
    Something like that.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    30

    Unhappy

    Can you show me the complete code?
    I still couldn't catch the solution you provided.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    903
    Lazy bastard. Ask questions, but do not ask us to do the whole damn job for you.

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    While your waiting for him to write the code, *cough*. You could read the rules perhaps?

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    30

    Thumbs up

    oops!! I m sory.

    Anyway, someone who declared the below stuff has just informed me, there was a mistake in the previous declarations.
    Check the below declaration:-

    Code:
    menu_item_t * * m_menuitem;
    
    should be declared as :-
    
    menu_item_t  *m_menuitem[5]; //Array of pointers

    Well, it seems much easier to me to handle Array of pointers in this case.

    Really thanks a lot to everyone.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Mmm, that makes more sense. With the [5] declaration your program doesn't have to mess around with dynamic memory allocation.

    Tell whoever wrote that code that they should think up some better names.

    Anyway, good luck.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Yeah. The naming is kinda confusing. We will correct them.

    1 quick question:

    Code:
    menuItem_t **ptr_1;
    
    and
    
    menuItem_t *ptr_1[];
    
    Are they the same?

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And where do you compile the second line?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 09:42 PM
  2. beginner plzz help urgent
    By sara101 in forum C Programming
    Replies: 11
    Last Post: 01-14-2007, 10:38 PM
  3. Linked List Need Help Urgent
    By ykchua in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:57 PM