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

时间:2012-12-25 16:53:16

标签: c

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

#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();
 }

1 个答案:

答案 0 :(得分:0)

该代码确实有效,而scanf确实可以正常工作。我会推荐你​​阅读一些

您是否阅读了documentation scanf()如何运作?

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

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

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

  

5 2.3

然后ptr->isdn将有5,ptr->price将有2.3。如果那不适合你,那么也许这不是你的所有代码,或者你可能会错过复制的东西?

相关问题