Thread: Structures in C Help Please!

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    166

    Structures in C Help Please!

    Code:
    /*      Complex Structures 6 Points
    
     Define pHead, a pointer to a structure called HEADER.
     This structure contains two fields: a counter (type int)
     and a pointer to an array of structures, called pAry.
    
     The base type of the pAry array is another structure called BOOK.
    
     This BOOK structure contains three fields: title (a dynamically allocated string),
     author (30 characters), and date of type DATE (a structure with three integers:
     month, day, and year).
    
           Requirements:
           1.      Type Definitions
           2.      In main(), define an array of 5 books and initialize it.
           3.      Write printBook(), a function that prints a book:
                     void printBook( const BOOK *p );
           4.      Write a function that prints the array of books using the printBook()
               function in a loop.
           5. Code a printf statement to print
                           - the title,
                           - the first letter of the author's name, and
                           - the year of the third book in the array.
    
           Save the output as a comment then upload the program.
    
     Written by:Anastasia Glyantseva
     Date: 6/5/2012
    
    // ====================================================== */
    #include <stdio.h>
    #include <string.h>
    #define MAX_TITLE 100
    
    void printBook( const BOOK *p);
    
    typedef struct
           {
           char title[MAX_TITLE];
           char author[30];
                   typedef struct
                    {
                           int month;
                           int day;
                           int year;
                    }DATE;
           } BOOK;
    typedef struct
           {
            int counter = 0;
            char *pAry = *BOOK;
           }HEADER;
    int main( void )
    {
    //  Local definitions
    BOOK list[5]=
    {
           {"Title1", "Author1", {1, 26, 2001}},
           {"Title2", "Author2", {2, 27, 2002}},
           {"Title3", "Author3", {3, 28, 2003}},
           {"Title4", "Author4", {4, 29, 2004}},
           {"Title5", "Author5", {5, 30, 2005}},
    };
    //  Statements
           HEADER head = {5, list};
           HEADER *pHead = &head;
           pHead -> pAry[2].title;
           *(pHead -> pAry[2].author);
           pHead->pAry[2].date.year;
    
    return ;
    }
    void printBook( const BOOK *p)
    {
    
    
    }
    -So we just started learning about this stuff in class so chances are, this code is horrible... But I need to finish this assignment(the instructions are the comment portion on the top). Some of the problems that I'm having right now is my compiler not liking all of the braces that I have in my BOOK structure initialization...But I can swear it looks just like that our teacher told us to do. Another problem is the pAry. It tells me that 'HEADER' has no member named pAry. But I don't know how to fix this because I put pAry in HEADER already.
    Anyway, thank you for your time and sorry if this code looks really stupid.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I can swear it looks just like that our teacher told us to do
    I'm ready to believe that your teacher made a bad mistake, but he didn't tell you to do that with the structure definitions as you've posted them.

    I don't know how to fix this because I put pAry in HEADER already.
    The `pAry' declaration is invalid. Is a `*BOOK' (Which by the way is itself a meaningless and invalid expression.) a `char *'?

    To fix all this crap, start at the top and work your way down. For example, the `DATE' label isn't a variable but a structure definition.

    Soma

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by phantomotap View Post
    I'm ready to believe that your teacher made a bad mistake, but he didn't tell you to do that with the structure definitions as you've posted them.



    The `pAry' declaration is invalid. Is a `*BOOK' (Which by the way is itself a meaningless and invalid expression.) a `char *'?

    To fix all this crap, start at the top and work your way down. For example, the `DATE' label isn't a variable but a structure definition.

    Soma
    The *BOOK was a typo. I meant for it to be &BOOK. But this still doesn't not remedy the problem. From what I understand, DATE is supposed to be a structure definition... Honestly, the part with all the braces was copied from what the teacher wrote on the board. Could you tell me what the problems are so that I could have a better chance of fixing them?

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Nope, but if you post the compiler errors you are getting and the latest code that you actually have someone here will be happy to help you understand what the compiler errors say so that you may understand the problems for yourself.

    Soma

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    I already posted the compiler errors... A bunch of them are warnings about the extra braces for BOOK list and the other one is an error that HEADER has no member named pAry. As for the code, beside the change from *BOOK to &BOOK nothing else is really changed cause I'm kind of already stuck with the errors that I have from the code already written.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by phantomotap View Post
    To fix all this crap, start at the top and work your way down.
    Actually I would start from the bottom and work my way up: first typedef DATE, using this new type to typedef BOOK, ...

    Bye, Andreas

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Damn your teacher in the pits of hell for teaching you typedef before teaching you how to use structs! Here's some awesome free advice: before you have a good grasp of typedef and struct, use struct blah everywhere.

    i.e. don't typedef struct BOOK to BOOK.

    just write "struct BOOK". As a beginner it will save you a ton of headaches from typedef-ing pointer types and other mumbo jumbo that most C instructors seem to preach with blissful ignorance.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAX_TITLE 100
    
    typedef struct
    {
           int month;
           int day;
           int year;
    }DATE;
    typedef struct
    {
           char title[MAX_TITLE];
           char author[30];
           DATE date;
    } BOOK;
    typedef struct
           {
            int counter;
            BOOK *pAry;
           }HEADER;
    void printBook( const BOOK *p);
    void printList( const BOOK *p, HEADER *pHead );
    
    int main( void )
    {
    
    //  Local definitions
    BOOK list[5]=
    {
           {"Title1", "Author1", {1, 26, 2001}},
           {"Title2", "Author2", {2, 27, 2002}},
           {"Title3", "Author3", {3, 28, 2003}},
           {"Title4", "Author4", {4, 29, 2004}},
           {"Title5", "Author5", {5, 30, 2005}}
    };
    //  Statements
    
           BOOK *p;
           HEADER head = {5, list};
           HEADER *pHead = &head;
    
           printList(p, pHead);
           printf("%s %c %d\n", pHead -> pAry[2].title, pHead -> pAry[2].author[0], pHead -> pAry[2].date.year );
    return ;
    }
    void printBook( const BOOK *p)
    {
    
    char title[MAX_TITLE];
    printf("%s %s %d %d %d\n", p -> title,
                            p -> author,
                            p -> date.month,
                            p -> date.day,
                            p -> date.year);
    
    return ;
    }
    void printList( const BOOK *p, HEADER *pHead )
    {
    
           for(p = pHead->pAry; p < pHead -> pAry + pHead -> counter ; p++)
           {
           printBook(p);
           }
    
    }
    /* OUTPUT:
    Title1 Author1 1 26 2001
    Title2 Author2 2 27 2002
    Title3 Author3 3 28 2003
    Title4 Author4 4 29 2004
    Title5 Author5 5 30 2005
    Title3 A 2003
    */
    -Here is the final version that seemed to work. Thank you everyone for your help. @claudiu: I will try to play around with just struct when I have some free time. Unfortunately, she keeps piling us with more homework, so for now, I have to move on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures as members of structures & offset
    By trish in forum C Programming
    Replies: 24
    Last Post: 07-16-2011, 05:46 PM
  2. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  3. Accessing Structures Inside Structures
    By Mellowz in forum C Programming
    Replies: 1
    Last Post: 01-13-2008, 03:55 AM
  4. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM