-2

I have a structure filed that I have created and am trying to get it to display in my main function. I keep getting the the error:

" error: initializer element is not constant
 struct childrensBooks *book1 = (struct childrensBooks *) malloc(sizeof(struct childrensBooks));  //Structure of book #1
                                                                           ^

lab2Structure.h:11:1: error: expected identifier or ‘(’ before ‘{’ token {

How can i fix this?? Here is my structure.h file:

struct childrensBooks           
{
    char *title;                //use of pointers to store memory for title
    char *author;               //use of pointers to store memory for author
    char *publisher;            //use of pointers to store memory for publisher
    int copyright;
    double price;
};

struct childrensBooks *book1 = (struct childrensBooks *)   malloc(sizeof(struct childrensBooks));       //Structure of book #1
{
book1->title = (char *)malloc(100);                                                             
book1->author = (char *)malloc(100);
book1->publisher = (char *)malloc(100);
book1->copyright = 1997;
book1->price = 8.99

memcpy(book1->title, "We're Going on a Bear Hunt", 26);
memcpy(book1->author, "Michael Rosen", 13);
memcpy(book1->publisher, "Little Simon", 12);
book1->copyright = 1989;
book1->price = 7.99;

fprintf(stderr, "%s was written by %s\n. Publisher is %s. Copyright %d\n. Retail price is $%.2d\n.", book1->title, book1->author, book1->publisher,
    book1->copyright, book1->price);

}

Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
Joseph Kraemer
  • 111
  • 1
  • 2
  • 12
  • 1
    I suspect that the structure is defined at file scope (as opposed to function scope). If so, what do the curly braces after the struct definition enclose? It doesn't seem to be a function. – M Oehm Sep 28 '15 at 14:37
  • I have multiple books. This is just *book1 but I have up to *book7. Do I not need these curly braces after each book? – Joseph Kraemer Sep 28 '15 at 14:39
  • The curly braces are used to initialise structures, not pointers to structures. In your example, the curlies don't enclose initialisation data, but code. – M Oehm Sep 28 '15 at 14:41
  • @JosephKraemer you need an input of how many books you have. Based on that you can allocate number of books and you can use a loop to enter information of books. Read some stuff about structures later yo can do this easily. http://www.tutorialspoint.com/cprogramming/c_structures.htm – Gangadhar Sep 28 '15 at 14:45
  • Please read a C book. This should answer your questions. If not, try a different book. If noe helps, try a different programming language you do not have to work with pointers. Python might be a better start for you. – too honest for this site Sep 28 '15 at 14:53
  • 1
    @Joseph, note that you don't need to cast the return value of malloc/calloc/realloc in C. Only in C++. – Johann Gerell Sep 28 '15 at 15:27

2 Answers2

0

You can't write executed code just like that outside function and in header file. H for definitions, C files for function bodies.

Andrey
  • 59,039
  • 12
  • 119
  • 163
0

You seem to be mixing up two different conventions for how to initialize memory for pointers. You are using a malloc call to allocate the memory like you plan on using it as a pointer as seen here Initializing a pointer to a structure but then you seem to be trying to group things together like this How to initialize a struct in accordance with C programming language standards.

Whichever you decide to use DO NOT define all of this information in the header file. I know it seems convenient and all but header files should really be left to only things loaded into the symbol table. To put more simply, if you are declaring a variable put it in a C file not an H file. (I am generalizing this a bit but for your case this seems appropriate).

Community
  • 1
  • 1
arduic
  • 659
  • 3
  • 12