-5

I'm struggling with dinamic memory allocation and I don't know what is wrong with the logic of the code below. Can someone please give me an explanation and change what's wrong.

struct Books {
char *title = new char[0];
char *author;
int pages = 0;
int price = 0;
};

int main()
{

struct Books book;

/*char size = (char)malloc(sizeof(book.title));*/

printf("The title of the book is:\n");
fgets(book.title, sizeof(book.title), stdin);

printf("The title is:\n %s", book.title);

}
  • 1
    Please elaborate more on what are you trying to do, what is the expected result and what is your current result – Andreas Dec 17 '18 at 09:52
  • 2
    That doesn't look like valid c to me. For example, "new" is not a C operator - it's C++. – Simon F Dec 17 '18 at 09:52
  • You can't initialize variables inside a struct definition. Remove all assignments from inside the struct. Then set `book.title = malloc( n );` where `n` is the size in bytes to allocate. It also helps if you know the difference between C and C++ and decide which language you are actually programming in. – Lundin Dec 17 '18 at 09:52
  • `new char[0]`? Really no byte desired? But you are reading `sizeof(book.title)` (size of the pointer, not the pointed to array) elements. That cannot work. – Werner Henze Dec 17 '18 at 09:55
  • There is a list of good C++ books [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Dec 17 '18 at 10:34

2 Answers2

1

Here's how to write your code so that it's legal C

struct Books {
    char *title;
    char *author;
    int pages;
    int price;

};

int main()
{

    struct Books book;

    book.title = malloc(100);

    printf("The title of the book is:\n");
    fgets(book.title, 100, stdin);

    printf("The title is:\n %s", book.title);

}

This would be covered in any book on C, you really should read one.

john
  • 85,011
  • 4
  • 57
  • 81
0

In general there are two ways to handle cases like this: You can either use char-arrays with a pre-defined size, in which case you must make sure to not write more characters than the array can hold into it. A code with pre-defined sized arrays would look like this:

struct Books {
    char title[255];
    char author[255];
    int pages;
    int price;
};

int main()
{

    struct Books book;

    printf("The title of the book is:\n");
    fgets(book.title, sizeof(book.title), stdin);

    printf("The title is:\n %s", book.title);
}

In the above case, it's valid to use sizeof(book.title) because the size is known at compile time. But "title" can never exceed 254 characters.

Another way would be using dynamic memory allocation:

struct Books {
    char * title;
    char * author;
    int pages;
    int price;
};

int main()
{

    struct Books book;
    book.title = NULL;
    size_t n = 0;

    printf("The title of the book is:\n");
    getline(&book.title, &n, stdin);

    printf("The title is:\n %s", book.title);

    free(book.title);
}

In this case, the getline() function allocates the memory for you, so there is no pre-defined maximum size of the string. But you have to free it yourself and also cannot use sizeof() to get the size of the array.

Georg
  • 94
  • 4