Thread: Need a litle help with Struct arrays

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    2

    Need a litle help with Struct arrays

    Hi. First of all, I'm new to the forum, so it's very nice to meet all of you who are reading this.

    I have some experience with Java programming, but I'm only now starting with C.

    So like the title suggests, I'm having a little trouble with my struct arrays.

    I'm just doing little programs to use as tests, so that I can get started on my bigger "homework" project.

    First of all I got something like this:

    Code:
    typedef struct book { char title[MAX_TITLE];
     char isbn[MAX_ISBN];
     char authors[MAX_AUTHORS];
     struct publisher * publ_ptr;
     int year;
    } Book[MAX_BOOKS];
    
    
    typedef struct publisher {
     char name[MAX_PUB_NAME];
     Book * book_ptr[MAX_PUB_BOOKS];
     int book_count;
    } Publisher[MAX_PUBS];
    So all the MAX limits stuff are already defined so no worries.
    So next I was trying to just access the structures, so my first objective was to fill the string title, of the struct Book, from standart input.

    So I did

    Code:
    fgets(&Book[1].title,MAX_LINE_CHAR, stdin);
    printf("%s\n",Book[1].title);
    So then I compile it in the console with
    gcc -Wall -o Ex3.out Ex3.c

    and I get the following errors
    Ex3.c:38: error: expected expression before ‘Book’
    Ex3.c:38: error: too few arguments to function ‘fgets’
    Ex3.c:39: error: expected expression before ‘Book’

    the error lines corresponding to the 2 lines of code that I posted.

    And I know that the problem is in accessing the variable title, because I already tested the fget function with a normal char array and worked perfectly.

    So can anyone enlighten me on what I'm doing wrong?

    Thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What you probably wanted to do was to define a struct named struct book and have Book be a typedef for the struct type:
    Code:
    typedef struct book {
        char title[MAX_TITLE];
        char isbn[MAX_ISBN];
        char authors[MAX_AUTHORS];
        struct publisher *publ_ptr;
        int year;
    } Book;
    Now, in say the main function, you can define a variable named say, books, that is an array of MAX_BOOKS Book objects:
    Code:
    Book books[MAX_BOOKS];
    With this array, you can then call fgets:
    Code:
    fgets(books[1].title, MAX_TITLE, stdin);
    Notice that I did not use the address-of operator& because books[1].title is itself an array which in this context will be converted to a pointer to its first element. I also used MAX_TITLE instead of MAX_LINE_CHAR since the title member was declared as having MAX_TITLE elements (one element must be reserved for the null character, but this is handled by fgets). By the way, note that fgets will store the newline character from entering the line if it is read, so you may need to search for it and replace it with a null character.

    Also, remember that array indices start from 0, and you probably wanted to use a loop anyway.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    2
    Thank you laser, it's working now, I guess I thought both our ways were interchangeable, so I didn't even tried doing it your way before, I just added the [MAX_BOOKS] because it was shorter that way, since I wouldn't need to do the Book books[MAX_BOOKS]; line.
    About the MAX_TITLE thing, I guess it's a nice detail to have even though MAX_LINE_CHAR and MAX_TITLE both have the value of 255.


    Thank you so much, you literally saved my semester! =D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. litle help with the compare of chars
    By wesdom in forum C Programming
    Replies: 11
    Last Post: 10-29-2013, 04:43 AM
  2. Struct Arrays
    By Jdo300 in forum C Programming
    Replies: 17
    Last Post: 03-09-2012, 03:16 PM
  3. arrays and struct !!
    By abood1190 in forum C Programming
    Replies: 9
    Last Post: 10-19-2011, 10:26 AM
  4. Replies: 1
    Last Post: 03-31-2011, 10:30 AM
  5. Ring of processes (litle test of comunication) !!!
    By husust in forum C Programming
    Replies: 1
    Last Post: 02-08-2006, 07:58 PM