简体   繁体   中英

return makes integer from pointer without a cast when returning a struct

Im writing a code for a school project where we are defining a library and I cant get my function to return correctly

I have tried to redefine my code and change int init_book to int *init_book but that only gives me other errors

int init_book(struct book *p_book, const char *p_id, const char *p_title, const char * p_author, struct date p_release) {
    p_book = malloc(sizeof(struct book));
    for (int i = 0; i < 10; i++) {
        p_book->id[i] = p_id[i];
    }
    p_book->title = malloc(strlen(p_title) * sizeof(char));
    p_book->author = malloc(strlen(p_author) * sizeof(char));
    p_book->title = p_title;
    p_book->author = p_author;
    p_book->release_date = p_release;
    return p_book;
}

//a part of my main function that initiates the function

if (init_book(&a1, "000000009", "Harry Potter and the Philosopher's Stone", "J. K. Rowling", a1date)) {
        printf("Initialization succeeded\n");
        printf("%s\n", a1.title); 
                //it prints initialization succeeded but not a1.title
    }
    else {
        printf("Initialization failed\n");
    }

You are attempting to return a struct book * but you declared the function as returning int (or int * ). This can't work.

You need to change the return type to struct book * . Next, remove the unnecessary p_book parameter and convert it into a local variable: It has no use. The result looks like this:

struct book *init_book(const char *p_id, const char *p_title, const char * p_author, struct date p_release) {
    struct book *p_book = malloc(sizeof *p_book);
    for (int i = 0; i < 10; i++) {
        p_book->id[i] = p_id[i];
    }
    p_book->title = malloc(strlen(p_title) + 1);
    p_book->author = malloc(strlen(p_author) + 1);
    strcpy(p_book->title, p_title);
    strcpy(p_book->author, p_author);
    p_book->release_date = p_release;
    return p_book;
}

(I've also fixed the errors in the string allocation and copying in your code: your code didn't allocate sufficient space, but the allocation was leaked anyway since you moved pointers instead of copying the contents.)

And it's called like this:

struct book *p_book = init_book("000000009", "Harry Potter and the Philosopher's Stone", "J. K. Rowling", a1date);

if (p_book) {
    printf("Initialization succeeded\n");
    printf("%s\n", a1.title); 
} else {
    printf("Initialization failed\n");
}

I'm going to take a different approach to the other answers. I think the return value of your function is intended to be a status rather than the initialized struct book since you are passing in a pointer to the struct book you wish to initialize. In that case, you should not be malloc ing the book because the pointer you are passing in should already be to a valid memory location of a struct book .

Use the return value to indicate whether an error has occurred in initialization.

Without knowing more about your struct book it's hard to tell if you need to malloc any of the member variables, or just assign pointers. If you do need to use malloc , then you can use the return code from your calls to malloc to set your own return value to indicate whether or not there was an error.

The return type of a function should match the type of whatever you're trying to return. So to return p_book , of type struct book * , then your return type should be struct book * .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM