gpt4 book ai didi

c - 如何拥有一个使用指针 ptr 来读取和打印有关书籍结构的内容的程序?

转载 作者:行者123 更新时间:2023-11-30 18:44:55 24 4
gpt4 key购买 nike

如何让程序使用指针 ptr 来读取和打印有关书籍结构的内容,而不使用任何其他变量。为了访问结构体的内容,我必须使用 ->我的程序在第一次读取后终止。有人可以给我一个代码吗? 这是我的代码:

#include<stdio.h>

struct book {
char title[100];
char authors;
int code;
double prc;
};

int main(void) {
struct book *ptr;
printf("Title:\n");
gets(&ptr->title[100]);
printf("authors:\n");
gets(&ptr->authors);
printf("code:\n");
scanf("%d",&ptr->code);
printf("Price:\n");
scanf("%lf",&ptr->prc);

printf("T:%s,A:%s,C:%d,P:lf\n",ptr->title,ptr->authors,ptr->code,ptr->prc);
return 0;
}

我希望程序读取关于一本书的这 4 件事,然后仅使用指针 ptr 在最终的 printf 中打印它们

最佳答案

您有几个问题

  • 您错过了分配图书实例

  • &ptr->title[100] 无效

  • 多年来 gets 的使用已被弃用,使用 fgets 来限制大小并避免未定义的行为

  • 您错过了检查 scanf 返回 1 以了解是否完成了有效输入

  • 由于没有freemalloc,您出现了内存泄漏,实际上您不需要在堆中分配您的书

  • 您确定仅一个字符对于作者来说就足够了吗?也许你想要一个数组

  • gets(&ptr->authors); 无效,因为 authors 只是一个字符

  • 作者的格式“%s”无效,因为它只是一个字符

  • 您在“lf”之前错过了 % 来打印价格

可能您不希望输入字符串中出现换行符

你想要这样的东西:

#include<stdio.h>
#include <stdlib.h>
#include <string.h>

struct book {
char title[100];
char authors[100];
int code;
double prc;
};

int main(void) {
struct book * ptr = malloc(sizeof(struct book));
size_t len;

printf("Title:\n");
if (fgets(ptr->title, sizeof(ptr->title), stdin) == NULL)
/* EOF */
return -1; /* or an other behavior is you want */
len = strlen(ptr->title);
if (ptr->title[len - 1] == '\n')
ptr->title[len - 1] = 0;
printf("authors:\n");
if (fgets(ptr->authors, sizeof(ptr->authors), stdin) == NULL)
/* EOF */
return -1;/* or an other behavior is you want */
len = strlen(ptr->authors);
if (ptr->authors[len - 1] == '\n')
ptr->authors[len - 1] = 0;
printf("code:\n");
if (scanf("%d",&ptr->code) != 1) {
puts("invalid code");
return 1; /* or an other behavior is you want */
}
printf("Price:\n");
if (scanf("%lf",&ptr->prc) != 1) {
puts("invalid price");
return 1; /* or an other behavior is you want */
}

printf("T:%s,A:%s,C:%d,P:%lf\n",ptr->title,ptr->authors,ptr->code,ptr->prc);

free(ptr);

return 0;
}

或者不在堆中分配书:

#include<stdio.h>
#include <string.h>

struct book {
char title[100];
char authors[100];
int code;
double prc;
};

int main(void) {
struct book b;
size_t len;

printf("Title:\n");
if (fgets(b.title, sizeof(b.title), stdin) == NULL)
/* EOF */
return -1; /* or an other behavior is you want */
len = strlen(b.title);
if (b.title[len - 1] == '\n')
b.title[len - 1] = 0;
printf("authors:\n");
if (fgets(b.authors, sizeof(b.authors), stdin) == NULL)
/* EOF */
return -1;/* or an other behavior is you want */
len = strlen(b.authors);
if (b.authors[len - 1] == '\n')
b.authors[len - 1] = 0;
printf("code:\n");
if (scanf("%d",&b.code) != 1) {
puts("invalid code");
return 1; /* or an other behavior is you want */
}
printf("Price:\n");
if (scanf("%lf",&b.prc) != 1) {
puts("invalid price");
return 1; /* or an other behavior is you want */
}

printf("T:%s,A:%s,C:%d,P:%lf\n",b.title,b.authors,b.code,b.prc);

return 0;
}

编译和执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra c.c
pi@raspberrypi:/tmp $ ./a.out
Title:
the mirific title
authors:
me
code:
007
Price:
12.34
T:the mirific title,A:me,C:7,P:12.340000
pi@raspberrypi:/tmp $

关于c - 如何拥有一个使用指针 ptr 来读取和打印有关书籍结构的内容的程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56305963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com