繁体   English   中英

我们可以通过结构指针扫描结构成员吗?

[英]can we scan structure members through structure pointers?

我已经尝试了下面编写的代码,但没有用。 我可以知道为什么吗? 是否可以通过结构指针扫描结构成员?

#include<stdio.h>
#include<conio.h>
struct book
{
  int isdn;
  float price;
 };
 struct book b,*ptr;
 void main()
 {
   clrscr();
   b.isdn=10;
   b.price=150.75;
   printf("\n%d %f",b.isdn,b.price);
   ptr=&b;
   printf("\n%d %f",ptr->isdn,ptr->price);
   scanf("%d %f",&ptr->isdn,&ptr->price); //this statement do not work,why?
   printf("\n%d %f",ptr->isdn,ptr->price);
   getch();
 }

该代码确实有效,并且 scanf 确实以这种方式工作。 我会推荐你​​阅读一些

您是否阅读了有关scanf()工作原理的文档

您需要完全按照格式字符串指定的方式传入数据。 所以在你的情况下:

scanf("%d %f",&ptr->isdn,&ptr->price); 

您需要传入一个整数、一个空格和一个浮点数,例如:

5 2.3

然后ptr->isdn将有 5 并且ptr->price将有 2.3。 如果您没有遇到这种情况,那么这可能不是您的全部代码,或者您错过了复制的内容?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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