C return在返回结构时从指针生成整数,而不使用强制转换
我正在为一个学校项目编写代码,在这个项目中,我们正在定义一个库,但我无法让我的函数正确返回 我试图重新定义我的代码,并将int init_book更改为int*init_book,但这只会给我带来其他错误C return在返回结构时从指针生成整数,而不使用强制转换,c,pointers,struct,malloc,C,Pointers,Struct,Malloc,我正在为一个学校项目编写代码,在这个项目中,我们正在定义一个库,但我无法让我的函数正确返回 我试图重新定义我的代码,并将int init_book更改为int*init_book,但这只会给我带来其他错误 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(sizeo
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");
}
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));
对于(int i=0;i<10;i++){
p_book->id[i]=p_id[i];
}
书名=malloc(strlen(书名)*sizeof(char));
p_book->author=malloc(strlen(p_author)*sizeof(char));
书名->书名=书名;
p_book->author=p_author;
采购订单->发布日期=采购订单发布;
还书;
}
//启动该功能的主要功能的一部分
如果(在书中(&a1,“00000009”,“哈利波特与魔法石”,“J.K.罗琳”,a1date)){
printf(“初始化成功\n”);
printf(“%s\n”,a1.标题);
//它打印初始化成功,但不打印a1.title
}
否则{
printf(“初始化失败\n”);
}
函数的返回类型应与您试图返回的内容的类型相匹配。因此,要返回p\u book
,类型为struct book*
,则返回类型应为struct book*
函数的返回类型应与您试图返回的内容的类型相匹配。因此,要返回p\u book
,类型为struct book*
,则返回类型应为struct book*
您试图返回一个struct book*
,但您将函数声明为返回int
(或int*
)。这不行
您需要将返回类型更改为structbook*
。接下来,删除不必要的p_book
参数并将其转换为局部变量:它没有任何用处。结果如下所示:
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;
}
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");
}
您正试图返回一个
struct book*
,但您将函数声明为返回int
(或int*
)。这不行
您需要将返回类型更改为structbook*
。接下来,删除不必要的p_book
参数并将其转换为局部变量:它没有任何用处。结果如下所示:
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;
}
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");
}
我将对其他答案采取不同的方法。我认为函数的返回值应该是一个状态,而不是初始化的
structbook
,因为您要传递一个指向要初始化的structbook
的指针。在这种情况下,您不应该malloc
ing该书,因为您要传递的指针应该已经指向struct book
的有效内存位置
使用返回值指示初始化过程中是否发生错误
如果不了解有关
struct book
的更多信息,就很难判断是否需要malloc
任何成员变量,或者只分配指针。如果您确实需要使用malloc
,那么您可以使用调用malloc
的返回代码来设置自己的返回值,以指示是否存在错误。我将对其他答案采取不同的方法。我认为函数的返回值应该是一个状态,而不是初始化的structbook
,因为您要传递一个指向要初始化的structbook
的指针。在这种情况下,您不应该malloc
ing该书,因为您要传递的指针应该已经指向struct book
的有效内存位置
使用返回值指示初始化过程中是否发生错误
如果不了解有关
struct book
的更多信息,就很难判断是否需要malloc
任何成员变量,或者只分配指针。如果确实需要使用malloc
,则可以使用调用malloc
的返回代码来设置自己的返回值,以指示是否存在错误。返回类型应与您返回的内容匹配。您不返回int
,也不返回int*
,那么为什么要尝试这些返回类型呢?您返回的是一个struct book*
,因此您的返回类型应该是…int init\u book(struct book*p_book,
表示函数返回int
p_book
是指针,那么您希望返回p_book;
做什么?如果您将实际struct book
的地址传递给函数,这一行表示您不初始化传递addres的结构向我们展示图书的定义。您可能不需要为图书结构中的每个字段使用malloc空间。//单图书条目结构图书{char id[10];const char*title;const char*author;struct date release\u date;}在struct book
中,title
和author
被声明为const char*
。这是作业的一部分还是您的想法?返回类型应该与您返回的内容匹配。您不返回int
,也不返回int*
,那么您为什么要尝试这些返回类型?您正在返回翻一个struct book*
这样你的返回类型应该是…int init\u book(struct book*p_book,
表示函数返回int
p_book
是指针,那么您希望返回p_book;
做什么?如果您将实际struct book
的地址传递给函数,这一行表示您不初始化传递addres的结构给我们看看书的定义。你可能不需要用malloc空间f