Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading struct values using pointer

Tags:

c

I have a struct that contains a float var. I am trying to read the value using a pointer to a struct. Here's the code:

struct mas {
    float m;
};

int main(void)
{
    struct mas *ms;
    ms=(struct mas*)malloc(sizeof(struct mas));
    scanf("%f",&(ms->m));
    printf("%f",ms->m);
    return 0;
}

But running the program produces the following error:

scanf floating point formats not linked

The compiler used is Borland Turbo C++ (3.0) on a Windows PC. Why is this so?

like image 230
Akash Avatar asked Jul 11 '26 00:07

Akash


2 Answers

This might be helpful: http://www.faqs.org/faqs/msdos-programmer-faq/part2/section-5.html

From the article:

Borland's compilers try to be smart and not link in the floating- point (f-p) library unless you need it. Alas, they all get the decision wrong. ... (To force them to link it) define this function somewhere in a source file but don't call it:

static void forcefloat(float *p)
{
  float f = *p;
  forcefloat(&f);
}

Also:

If you have Borland C++ 3.0, the README file documents a slightly less ugly work-around. Insert these statements in your program:

 extern unsigned _floatconvert;
 #pragma extref _floatconvert
like image 55
Timothy Jones Avatar answered Jul 14 '26 02:07

Timothy Jones


Why is this so..

Because there's a bug in your ancient, useless compiler. Upgrade to a newer one that properly handles floating point operations.

The latest version of GCC is a good choice, or you can download Microsoft's Visual C++ Express package for free, which bundles their compiler with a world-class IDE.

like image 23
Cody Gray Avatar answered Jul 14 '26 02:07

Cody Gray