- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何让程序使用指针 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 以了解是否完成了有效输入
由于没有free的malloc,您出现了内存泄漏,实际上您不需要在堆中分配您的书
您确定仅一个字符对于作者来说就足够了吗?也许你想要一个数组
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/
揭开网络爬虫的神秘面纱 嗨,亲爱的读者朋友们,今天我想和大家分享一下我对Python3网络爬虫书籍的一些心得体会。网络爬虫就像是一位善于探险的探险家,穿越数字世界的丛林,捕捉着信息的脉搏。 历经荆棘,
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我是 JavaScipt 新手,我无法理解下面的代码 maskat.lang.Class.declare("maskat.key.KeyEventManager", { _static: {
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭11 年前。 Improve th
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 5 年前。
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在开发一个应用程序,允许用户用户查看他们有权访问的收件箱的内容。除了 之外,我很难找到一种搜索全局地址列表的方法 AddressEntries entries = global.AddressEn
人们常常问我有什么C++和编程的书籍推荐,今天就为大家分享了几本 第一个注意项:如果你打算学习C++,请务必学习最新版的C++ 2011。这个版本的C++移除了许多由C++强大带来的一些痛苦之处。
我需要为一本书创建一个索引。虽然乍一看这项任务很简单——按第一个字母对单词进行分组,然后对它们进行排序——但这个显而易见的解决方案仅适用于美国语言。然而,真正的词要复杂得多。参见 http://en.
我是一名开发人员,将我们的 IIS 服务器的管理职责交给了他。在设置网站、虚拟目录等方面,我对 IIS 相当熟悉,但我想如果我要完全成为服务器的管理员,我最好尽快跟上。 有什么关于 IIS 书籍的推荐
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
如果我在网站上看到很酷的效果,我会查看 jquery 的源代码,通常我可以理解它,但是,我正在查看 http://www.mckinsey.com 的 slider 。并查看了用于进行页面幻灯片的 j
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
方法(对象的函数)可以使用“this”关键字来引用对象的变量。 函数的属性可以引用函数变量吗? 例如:- function foo() { var x=5; }
我有以下型号。 Book has Articles (Article has foreign key to Book) Article has Images (Article has upto #ma
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
Closed. This question needs to be more focused。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅关注editing this post的一个问题。
我是一名优秀的程序员,十分优秀!