C/Structure problem in c
Expert: Prince M. Premnath - 5/5/2010
QuestionI am running following C program and get message as below.
struct record
{
char name[10];
char addr[10];
int id;
float salary;
};
#include <stdio.h>
void main()
{
typedef struct record employee;
employee emp[5];
employee *ptr;
int i;
clrscr();
ptr=emp;
printf("Enter the name, address, id and salary for %d employees\n",5);
for (i=0;i<5;i++)
{
scanf("%f%s%s%d",&ptr->salary,ptr->name,ptr->addr,&ptr->id);
}
getch();
}
/*output
Enter the name, address, id and salary for 5 employees
scanf : floating point formats not linked
Abnormal program termination
*/
AnswerHai Dear ASHEERVAD!
Sorry for the delay in responding you , i apologize .
Usually this error occur only with Borland's Turbo C/ C++ compiler ,it doesn't always handle expressions such as &ptr->var ( -> has higher precedence than &) correctly. This can lead to a runtime error with the program crashing with an error message: floating point formats not linked.
a possible fix for this issue is just scan the values into a temp struct variable ( not structure pointer as you have done ) and assign / copy the values to the pointer
Some cases the floating point library may failed to link , in such cases we must force the compiler to link it
A very smart solution for floating point format linking issues id just add the code / function
static void forcefloat(float* p)
{
float f = *p;
forcefloat(&f);
}
some where in your source code ( don't wanna call this function )
eg: ive modified your code , nd i found it to be working , please find the sample below
static void forcefloat(float* p)
{
float f = *p;
forcefloat(&f);
}
struct record
{
char name[10];
char addr[10];
int id;
float salary;
};
#include <stdio.h>
void main()
{
typedef struct record employee;
employee emp[5];
employee *ptr;
int i;
clrscr();
ptr=emp;
printf("Enter the name, address, id and salary for %d employees\n",5);
for (i=0;i<5;i++)
{
scanf("%f%s%s%d",&ptr->salary,ptr->name,ptr->addr,&ptr->id);
}
getch();
}
Please get back to me i case of issues
Thanks and Regards
Prince M. Premnath