C Programming help .....

Roger Oberholtzer roger
Tue Jan 25 10:36:05 PST 2005


On Tue, 2005-01-25 at 16:07, Ben Duncan wrote:
> Ok, I need to to know how to copy/duplicate something in
> a structure.
> 
> Given:
> ---------------------------------------------------------
> 
> typedef struct _mastermenu
>         {
>           int  MM_lineid ;
>           char MM_item [16] ;
>           char MM_menu [8] ;
>         } MasterMenu ;
> 
>    MasterMenu MasterItems [8] = {
>         1, "Daily", "DAILY",
>         2, "Reports", "REPORT",
>         3, "Periodical", "PERIOD",
>         5, "Master Files", "MFILES",
>         6, "App Setup", "SETUP",
>         8, "Utilties", "UTILS",
>         9, "System", "SYSTEM",
>         10, "Jump to", "JUMP"
>       } ;
> 
>    struct _extended_menu
>       {
>         int somenumber ;
>         char a_char ;
>         char a_buffer [80] ;
>         MasterMenu [10] ;
>        }
> 
>     struct _extended_menu A_TEST_MENU ;
>     struct _extended_menu A_ARRAY_MENU [20] ;
> 
> ------------------------------------------------------------
> 
> Where _mastermenu/MasterMenu is just a menu set.
> MasterItems is the "Master" start set.
> _extended_menu is a structure to contain MasterMenu
> A_TEST_MENU is a single extened menu, A_ARRAY_MENU is an
> array of extended_menu for 20 .
> 
> I want to copy MasterItems into:
> 
> A:       A_TEST_MENU.Mastermenu[0]
> B:       A_ARRAY_MENU[0].MasterMenu[0]
> 
> So, what is the syntax for doing the copy?

I'm not sure what is getting copied where, but it could be something
like:

A: memcpy(&(A_TEST_MENU.Mastermenu[0]),
		&MasterItems, sizeof(MasterItems));

B: memcpy(&(A_ARRAY_MENU[0].Mastermen[0]),
		&MasterItems, sizeof(MasterItems));


> 
> ALSO .... Can and what would be the procedure if I wanted to
> set A_TEST_MENU and A_ARRAY_MENU to NULLS to make sure they
> contained nothing?

memset(&A_TEST_MENU, sizeof(struct _extended_menu), 0);

The sizeof() part can also be a variable name if it is a constant size,
like:

memset(&A_ARRAY_MENU, sizeof(A_ARRAY_MENU), 0);

> 
> Thanks ...
+????????????????????????????+???????????????????????????????+
? Roger Oberholtzer          ?   E-mail: roger at opq.se        ?
? OPQ Systems AB             ?      WWW: http://www.opq.se/  ?
? Nybrogatan 66 nb           ?    Phone: Int + 46 8   314223 ?
? 114 41 Stockholm           ?   Mobile: Int + 46 733 621657 ?
? Sweden                     ?      Fax: Int + 46 8   314223 ?
+????????????????????????????+???????????????????????????????+



More information about the Linux-users mailing list