Akash
Akash

Reputation: 5012

Reading struct values using pointer

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?

Upvotes: 3

Views: 1243

Answers (3)

Cody Gray
Cody Gray

Reputation: 244692

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.

Upvotes: 3

Alex Reynolds
Alex Reynolds

Reputation: 96927

I am able to compile and run this code under GCC 4.2.1:

#include <stdlib.h>
#include <stdio.h>

struct mas{
    float m;
};

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

Are you missing any #include statements? I don't think you need to cast the result from malloc().

Upvotes: 1

Timothy Jones
Timothy Jones

Reputation: 22125

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

Upvotes: 5

Related Questions