C 如何有一个程序使用指针ptr来读取和打印有关书籍结构的内容?
如何让一个程序使用指针ptr来读取和打印有关书籍结构的内容,而不使用任何其他变量。为了访问结构的内容,我必须使用-> 我的程序在第一次读取后终止。 有人能给我一个密码吗? 这是我的密码:C 如何有一个程序使用指针ptr来读取和打印有关书籍结构的内容?,c,pointers,struct,C,Pointers,Struct,如何让一个程序使用指针ptr来读取和打印有关书籍结构的内容,而不使用任何其他变量。为了访问结构的内容,我必须使用-> 我的程序在第一次读取后终止。 有人能给我一个密码吗? 这是我的密码: #include<stdio.h> struct book { char title[100]; char authors; int code; double prc; }; int main(void) { struct book *ptr; p
#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中打印它们。你必须首先分配内存
struct book *ptr = malloc(sizeof (*ptr));
另外,永远不要使用函数gets。它已被移除
此外,对gets的调用是错误的。参数应该是ptr->title。但是用这个来代替:
fgets(ptr->title, 100, stdin);
并始终检查scanf的返回值,以确保一切正常。您有几个问题 您没有分配您的图书实例 &ptr->标题[100]无效 GET的使用多年来一直被弃用,请使用FGET限制大小并避免未定义的行为 您没有检查scanf return 1以了解输入是否有效 你有一个内存泄漏,因为malloc没有空闲,事实上你不需要在堆中分配你的书 你确定只需要一个字符就足够了吗?可能你想要一个数组 获取&ptr->authors;无效,因为作者只是一个字符 作者的格式%s无效,因为它只是一个字符 在打印价格之前,你错过了% 可能您不希望在输入字符串中使用换行符 你想要的是:
#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;
}
分配您的书本,或者不要使用指针将其放置在堆栈中。不要使用gets,使用fgets限制大小,验证scanf return 1以检查用户输入的有效输入。永远不要使用,其使用率会降低。改用fgets。另外,这个&ptr->title[100]是错误的。由于标题是字符缓冲区,其名称本身就是地址,请使用scanf%s,ptr->title;或fgetsptr->title,sizeofptr->title,stdin;并检查要使用的fgets或scanf的返回值。gets不仅不推荐使用。它在2011年被删除。@Broman不幸的是,它没有被完全删除,在gcc版本6.3中仍然可以使用它,有一个警告,但链接“找到它:-我在使用第一个解决方案时遇到了很多编译错误,yeah gets仍然可以使用devc++@bruno,这是真的。但它不再是标准的一部分。@Achal对于小程序来说并不是真正必要的,我认为如果我能解决所有的缺陷,答案将变得更广泛。
#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 $