简体   繁体   中英

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. I'll refer you to read some

Did you read the documentation on how scanf() works?

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

Then ptr->isdn will have 5 and ptr->price will have 2.3. If that's not happening for you then perhaps this isn't all your code, or maybe you miss copied something?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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