easy structure question

M

Michael

Hi,

In some code in my header file I have:

typedef struct{
int book_id;
char title[MAX_TITLE_SIZE];
char author[MAX_AUTHOR_SIZE];
int year;
int price;
int quantity;
}Book;

then later on I have:

Book *new_book;
char temp_title[MAX_TITLE_SIZE];
char temp_author[MAX_AUTHOR_SIZE];

if((new_book = malloc(sizeof(Book))) == NULL){
printf("Error allocating memory for new book");
}
new_book.book_id = new_book_id;

When I compile this I get errors that say "request for member book_id in
somehing not a structure or union".

I dont understand. I have Book as a structure and I have made new_book type
Book.

Can anyone help me here please....

Regards

Michael
 
I

Ian Collins

Michael said:
Hi,

In some code in my header file I have:

typedef struct{
int book_id;
char title[MAX_TITLE_SIZE];
char author[MAX_AUTHOR_SIZE];
int year;
int price;
int quantity;
}Book;

then later on I have:

Book *new_book;
char temp_title[MAX_TITLE_SIZE];
char temp_author[MAX_AUTHOR_SIZE];

if((new_book = malloc(sizeof(Book))) == NULL){
printf("Error allocating memory for new book");
}
new_book.book_id = new_book_id;
new_book->book_id = new_book_id;

new_book is a pointer to Book, not a Book
 
M

Martin Ambuhl

Michael wrote:
[...[
When I compile this I get errors that say "request for member book_id in
somehing not a structure or union".

I dont understand. I have Book as a structure and I have made new_book type
Book.

No, you have not "made new_book type Book." The type of new_book is
Book * (or, to avoid a frequent newbie confusion, the type of *new_book
is Book)

After turning your fragment into something compilable, I have the
following with some notes embedded.

#include <stdio.h>
#include <stdlib.h>
#define MAX_TITLE_SIZE 4096
#define MAX_AUTHOR_SIZE 4096

typedef struct
{
int book_id;
char title[MAX_TITLE_SIZE];
char author[MAX_AUTHOR_SIZE];
int year;
int price;
int quantity;
} Book;

int main(void)
{
Book *new_book; /* mha: note that new_book is a pointer
to a Book, not a Book */
int new_book_id = 3;

if (!(new_book = malloc(sizeof *new_book))) {
puts("Error allocating memory for new book");
exit(EXIT_FAILURE);
}
#if 0
/* mha: if new_book were a Book, the following would have been a way
to refer to the member book_id. But new_book is a
pointer-to-Book, not a Book. */
new_book.book_id = new_book_id;
/* mha: here's one way to do it */
(*new_book).book_id = new_book_id;
#endif
/* mha: and here's another */
new_book->book_id = new_book_id;

return 0;
}
 
M

Michael

Michael said:
Hi,

In some code in my header file I have:

typedef struct{
int book_id;
char title[MAX_TITLE_SIZE];
char author[MAX_AUTHOR_SIZE];
int year;
int price;
int quantity;
}Book;

then later on I have:

Book *new_book;
char temp_title[MAX_TITLE_SIZE];
char temp_author[MAX_AUTHOR_SIZE];

if((new_book = malloc(sizeof(Book))) == NULL){
printf("Error allocating memory for new book");
}
new_book.book_id = new_book_id;

When I compile this I get errors that say "request for member book_id in
somehing not a structure or union".

I dont understand. I have Book as a structure and I have made new_book
type Book.

Can anyone help me here please....

Regards

Michael


Thanks guys, I hope I 'get' this in the end. Lots of struggling to go I
suspect....Thanks for your help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top