1

Recently, I took a subject called Data structure. I've created a program to try out my knowledge but don't know why the program did not work. I can't figure it out so I post it here to ask for solution. I hope people can help me. I am newbie here. So please ignore my opinion if my opinion is found annoying.

#include <stdio.h>
int main()
{
    struct Book
    {
        char title[50];
        int year;
        float price;
    };
    int i;

    struct Book books[50];
    books[0].title="Bullshit";
    books[0].year=132;
    books[0].price=146.9;

    books[1]=(struct Book){"Money",1344,189.4
    };

    for(i=0;i<2;i++)
    {
        printf("Book Title is : %s\n",books[i].title);
        printf("Book Year is %d\n",books[i].year);
        printf("Book price is %3.2f\n",books[i].price);
        printf("\n\n");
    }
}
5
  • 13 16 D:\data structure code\Module self check 2.6.c [Error] assignment to expression with array type Commented Mar 12, 2018 at 15:06
  • 2
    Please edit the question with all relevant data (e.g. Compilation errors) instead of adding them in the comments. Commented Mar 12, 2018 at 15:13
  • books[0].title="Bullshit"; Really? Commented Mar 12, 2018 at 15:17
  • books[1] = {"Money",1344,189.4f}; you can initialise this directly Commented Mar 12, 2018 at 15:37
  • your books title suggests you are reading the wrong books. Try one of those instead : stackoverflow.com/questions/388242/… Commented Mar 12, 2018 at 16:06

2 Answers 2

0

1 I would make declaration of struct rather outside main than inside

2 Try changing char title[50] to char *title

#include <stdio.h>

struct Book {
    char *title;
    int year;
    float price;
};

int main() {

    int i;

    struct Book books[50];
    books[0].title = "Bullshit";
    books[0].year = 132;
    books[0].price = 146.9;

    books[1] = (struct Book) {"Money", 1344, 189.4
    };

    for (i = 0; i < 2; i++) {
        printf("Book Title is : %s\n", books[i].title);
        printf("Book Year is %d\n", books[i].year);
        printf("Book price is %3.2f\n", books[i].price);
        printf("\n\n");
    }
}

Why it didn't worked before?

In c arrays are not assignable by = operator.
You could do something like instead title[0] = 'B'; title[1] = 'u', etc....(or use strcpy which does it for you).

char *x is not really an array, it's just pointer to single char.
If we write x = "abc", we are telling the compiler: set x to 'a', next byte to 'b', next to 'c', and next to 0(not '0', just zero).

And when you do printf("%s",x), the printf function prints chars from the place in memory specified by x until it see 0 byte.

char *x = "abcd";

char *y = x;
while(*y != 0){   // this loop acts like printf("%s",x);
    printf("%c",*y);
    y++;
}

See also this and this question.

Or if you are using c++, not c, use std::string:

#include <cstdio>
#include <string>

struct Book {
    std::string title;
    int year;
    float price;

    Book(std::string t, int y, float p) {
        title = t;
        year = y;
        price = p;
    }
};

int main() {

    int i;

    Book books[50];
    books[0].title = "Bullshit";
    books[0].year = 132;
    books[0].price = 146.9;

    books[1] = Book(std::string("Money"), 1344, 189.4);

    for (i = 0; i < 2; i++) {
        printf("Book Title is : %s\n", books[i].title.c_str());
        printf("Book Year is %d\n", books[i].year);
        printf("Book price is %3.2f\n", books[i].price);
        printf("\n\n");
    }
}
Sign up to request clarification or add additional context in comments.

14 Comments

If you are suggesting a change to title data structure, recommend std::string.
Also, the OP could use strncpy rather than assignment or changing the array to a pointer.
@ThomasMatthews yes, of course std::string, but this guy is writing in c, not c++(hmm... he didn't gave any information about it, but his code is more like c code than c++, that's why I didn't use std::string)
@JK Well, if he would be writing in C, the question would be tagged as C, instead of C++.
@AlgirdasPreidžius Original question hasn't got any tag about language, this tag has been added by edit.
|
0

this

books[0].title="Bullshit";

is not valid. title is defined as char[50]. Either do

strcpy(books[0].title, "BS");

This will copy the bytes of BS to title. Or do

 struct Book
    {
        char *title;
        int year;
        float price;
    };

...
  books[0].title = strdup("BS");

This sets title up as a pointer to a char string. strdup will allocate space for the string and copy BS to that space. You will have to free that allocated space.

Or - the best. Use std::string

 struct Book
    {
        std::string title;
        int year;
        float price;
    };
....
   books[0].title = "BS";

And as a final thought - life goes better with std::vector instead of raw arrays

2 Comments

"And as a final thought - life goes better with std::vector instead of raw arrays" - 100% agree ;)
std::vector and std::string are c++ things. I see the tag has been changed to c. SO ignore those suggestions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.