You are here:

C/Structure problem in c

Advertisement


Question
I 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
*/

Answer
Hai 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  

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise


I'm sure that I can solve any doubts in Turbo C ,Graphics Programing ,Mouse, Hardware Programming ,File System ,Interrupts, BIOS handling , TSR Programming , General Concepts in C Language, handling inline Assembly statements

Experience

Research over 6+ Years

Organizations
CG-VAK Softwares and Exports Limited

Education/Credentials
Masters in Computer Applications

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