What am I doing wrong? (struct)

Asked

Viewed 59 times

1

struct book{
    char code[5];
    char title[30];
    char category[15];
    char author[30];
    float price;
};

struct book book[100];
book[0].price = 1;

You’re making a mistake on the last line

  • 1

    I see no problems: http://ideone.com/Aps1ic

3 answers

0

You don’t need the word struct in the variable declaration.

#include <stdio.h>

int main(){
    struct book{
        char code[5];
        char title[30];
        char category[15];
        char author[30];
        float price;
    };

    book books[100];
    books[0].price = 1.0;
    printf("%f/n" ,books[0].price);
    return 0;
}

0

Try to state your struct as follows:

struct book{
char code[5]; 
char title[30]; 
char category[15]; 
char author[30]; 
float price; 
}sBook;

sbook book[100]; 
book[0].price = 1;

I hope I’ve helped!

0

To resolve change the vector name, for example, to Books.

struct book books[100];

Then when assigning the value 1 to a float do so:

books[0].price = 1.0;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.