You are here:

C/pointers to structures

Advertisement


Question
sir,

could u please tell me the method to declare a float type variable in a structure and read the values and print them in the main.

the program is giving me an error 1.e
scanf:floating point format link error
Abnormal program termination
the program is as follows:-
main()
{struct complex
{ float x;
float y;
};
struct complex *ptr;
printf("Enter the numbers
");
scanf("%f%f",(*ptr).x,(*ptr).y));
printf("x=%f  y=%f",ptr->x,ptr->y);
}

Answer
Here is the corrected code:
main()
{
       struct complex
       {
               float x;
               float y;
       };
       struct complex *ptr = (struct complex *) malloc(1 * sizeof(struct complex));;

       printf("Enter the numbers => ");
       scanf("%f%f", &((*ptr).x), &((*ptr).y));
       printf("x=%f  y=%f",ptr->x,ptr->y);
}

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.