Thread: need help about structure..Thanks

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    36

    need help about structure..Thanks

    hi..i am very beginner with c structure and i don't how to deal with complex struct as follow
    Code:
    typedef struct saleStruct
    {
       struct tm date;
       int quantity;
       float price;
       struct saleStruct* next;
    } SaleType;
    
    typedef struct bookStruct
    {
       int id;
       char title[TITLE_LEN + 1];
       char author[AUTHOR_LEN + 1];
       int year;
       float price;
       int count;
       SaleType* head;
       struct bookStruct* next;
    } BookType;
    
    typedef struct bookListStruct
    {
       BookType* head;
       int count;
    } BookListType;
    
    Code:
    /* can i do like this?? */
     BookType *head = NULL, *current, *previous;
    how do i need to access through BookType id using
    BookListType *booklist , i had try using "booklist->BookType"
    but fail or maybe wrong..can gave some tips..Thanks...

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    If you wanted to access the BookList structure from a pointer to the BookListType structure simply do something like:
    Code:
    BookListType* bookList = NULL;
    BookType* example = malloc(sizeof(BookType));
    bookList->head = example;
    bookList->head->id = 5;
    Make sure you have properly malloc'ed the space for where you are pointing bookList though, otherwise you will most likely get a seg fault.
    The cost of software maintenance increases with the square of the programmer's creativity.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So have you read up on linked lists yet?
    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.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    oo..ok..thanks..alot

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    20
    Code:
    BookListType* bookList = NULL;
    BookType* example = malloc(sizeof(BookType));
    bookList->head = example;
    bookList->head->id = 5;
    I have read throught link list, but I was wondering how can one insert values into the next node from BookListType structure.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057

    Question

    You mean like this?

    Code:
    BookListType booklist;  /* create a book list */
    BookType *book;    /* pointer to a book */
    
    /* ... initialize booklist, add data in ... */
    
    book = &booklist.head;  /* set bok to the beginning of the linked list */
    
    while(book) {  /* while the end of the list has not been reached */
        printf("%s", book->title);  /* print the title of the book */
        /* ... print the other data members ...*/
        book = book->next;    /* advance to the next book */
    }
    Whatever you do, don't forget to free everything you malloc()ed at the end with free().

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    ok..thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM