简体   繁体   English

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

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

i have tried the below written code but it did not work.我已经尝试了下面编写的代码,但没有用。 may i know why?我可以知道为什么吗? is it possible to 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();
 }

That code does work, and scanf does work that way.该代码确实有效,并且 scanf 确实以这种方式工作。 I'll refer you to read some我会推荐你​​阅读一些

Did you read the documentation on how scanf() works?您是否阅读了有关scanf()工作原理的文档

You need to pass in the data exactly as the format string specifies.您需要完全按照格式字符串指定的方式传入数据。 So in your case:所以在你的情况下:

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

You need to pass in an integer, a space, and a float, for example:您需要传入一个整数、一个空格和一个浮点数,例如:

5 2.3 5 2.3

Then ptr->isdn will have 5 and ptr->price will have 2.3.然后ptr->isdn将有 5 并且ptr->price将有 2.3。 If that's not happening for you then perhaps this isn't all your code, or maybe you miss copied something?如果您没有遇到这种情况,那么这可能不是您的全部代码,或者您错过了复制的内容?

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

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