Thread: structs

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    structs

    hey guys just wondering how i could access a member from the structure definitions below but access it from a function called systemInit like the one below.

    iv tried going menu->PriceType->dollars; but that wont work it says there is no member.

    And 1 other question if i needed to intialize the struct members which ones would i need to initialize this eventually is also going to be used for linked lists?

    Code:
    typedef struct category* CategoryTypePtr;
    typedef struct item* ItemTypePtr;
    
    /* Structure definitions. */
    typedef struct price
    {
       unsigned dollars;
       unsigned cents;
    } PriceType;
    
    typedef struct item
    {
       char itemID[ID_LEN + 1];
       char itemName[MAX_NAME_LEN + 1];
       PriceType prices[NUM_PRICES];
       char itemDescription[MAX_DESC_LEN];
       ItemTypePtr nextItem;
    } ItemType;
    
    typedef struct category
    {
       char categoryID[ID_LEN + 1];
       char categoryName[MAX_NAME_LEN + 1];
       char drinkType;      /* (H)ot or (C)old. */
       char categoryDescription[MAX_DESC_LEN];
       CategoryTypePtr nextCategory;
       ItemTypePtr headItem;
       unsigned numItems;
    } CategoryType;
    
    typedef struct gjc
    {
       CategoryTypePtr headCategory;
       unsigned numCategories;
    } GJCType;
    Code:
    void systemInit(GJCType* menu)
    {
    }

  2. #2
    Registered User vinit's Avatar
    Join Date
    Apr 2006
    Location
    India
    Posts
    39
    try this menu->headCategory->headitem->prices[index].dollars

  3. #3
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    You need to go with the actual name of the variable because PriceType is the type so it would be
    menu->headCategory->headItem->prices[somenumberhere]->dollars
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  4. #4
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    i get headitem that there is no member

  5. #5
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    it works but how come when i go ->dollars it dosnt like the -> instead i have to put the

    full stop .dollars im using ansi c on unix

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    remember the use 0f -> is when u access the structure members using a pointer of type that structure

    and . is used when access the structure members using a normal variable of type that structure

    for example
    Code:
    struct somename
    {
         int somedata1;
         int somedara2;
    };
    
    //using normal varibale
    struct somename name;
    
    //uisng structure pointer
    struct somename *nameptr;
    
    //access that data
    name.somedata1;
    name.somedata2;
    
    nameptr->somedata1;
    nameptr->somedata2;
    ssharish2005

  7. #7
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    also mate if i declare the member like this
    menu->headCategory->headItem->prices[somenumberhere]->dollars = 0

    how come i get a segmentation error?

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    what is your array size
    Code:
    PriceType prices[NUM_PRICES];
    i mean NUM_PRICES value ->????

    it seems that u crossing the array boundary

    ssharish2005

  9. #9
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    the array size of NUM_PRICES is 3

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    so what is the value of somenumberhere -> ???

    if this is more than 3 u might well get some error like SEG FAULT

    ssharish2005

  11. #11
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    menu->headCategory->headItem->prices[3]->dollars = 0

    thats how it is but i get a seg fault

  12. #12
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    given the declarations from the first post
    Code:
    menu->headCategory->headItem->prices[somenumberhere]->dollars = 0
    shouldn't compile
    Kurt

  13. #13
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    menu->headCategory->headItem->prices[somenumberhere].dollars = 0

    i changed it to this and now it does compile

  14. #14
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I know.

  15. #15
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    but why when i runnit do i get a seg fault

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  3. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 01:46 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 05:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM