Thread: how to ordered const structs

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    2

    how to ordered const structs

    Hello every one.
    I'm developing a menu system for a embedded device. i designed a struct and some core functions. i need all menu items in ordered some way. but when i try to compile this code faced with error { initializer element is not a constant }. here is sample code with comments. how can i solve this problem.
    (complier gcc)

    Code:
    #include <stdio.h>
    
    typedef struct _MENUITEM {			// this struct have details of menu items
    	unsigned char* title;			// title bar
    	struct _MENUITEM * topItem;		// if this item have parent, pointer of this parent, if dont have 0
    	struct _MENUITEM * subItems;		// if this item have sub items, pointer of first item, if dont have subitems 0
    } _menuItem; 
    
    #define ITEM_1_IND	0	// order of menu items
    #define ITEM_2_IND	1
    #define ITEM_3_IND	2
    #define ITEM_4_IND	4
    #define ITEM_5_IND	5
    #define ITEM_6_IND	6
    #define ITEM_7_IND	8
    
    const _menuItem itemOne;	// definitions
    const _menuItem itemTwo;
    const _menuItem itemThree;
    const _menuItem itemFour;
    const _menuItem itemFive;
    const _menuItem itemSix;
    const _menuItem itemSeven;
    
    _menuItem * const itemsArray[] = {	// now i can reach items ordered
    								&itemOne,
    								&itemTwo,
    								&itemThree,
    								0,
    								&itemFour,
    								&itemFive,
    								&itemSix,
    								0,
    								&itemSeven,
    								0};
    // error in this line. {initializer element is not a constant}
    const _menuItem itemOne =		{"one",		0,		itemsArray[ITEM_2_IND]};
    // one have no parent item and it have subitem(s). this items start from second(ITEM_2_IND=1) item of itemsArray
    const _menuItem itemTwo =		{"two",		itemsArray[ITEM_1_IND],		itemsArray[ITEM_4_IND]};
    const _menuItem itemThree =		{"three",	itemsArray[ITEM_1_IND],		itemsArray[ITEM_7_IND]};
    
    int main(void) {
    	
    	printf ("hello");
    	return EXIT_SUCCESS;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why not
    Code:
    const _menuItem itemOne =		{"one",		0,		&itemTwo};
    and dispense with the itemsArray.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2
    thanks Salam. it's ok now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM