• C

Problem with dynamic arrays and scanf

The following program stops running with the error "scanf: floating point formats not linked". Does anyone know why?

  #include <stdio.h>
  #include <alloc.h>
  #include <mem.h>

  void main(void)
  {
    double **array;
    int i, j;

    array = (double**)malloc(sizeof
      double*) * 2);
    for (i = 0; i < 2; i++)
    {
      array[i] = (double*)malloc(sizeof
        double) * 2);
    }

    for (i = 0; i < 2; i++)
    {
      for (j = 0; j < 2; j++)
      {
        printf("Input [%d][%d]: ",
          i, j);
        scanf("%lf", &array[i][j]);
      }
    }
  }

P.S. I know I could replace the scanf   line with something like

  double* x;
  x = &str[i][j];
  scanf("%lf", x);

which works, but I don't want to use this. It's C, so it's supposed to flexible.
badea_andreiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zebadaCommented:
I think I remember years ago (on unix anyway) you need to link in libcm.lib or libmc.lib or libm.lib or something like that to get floating point stuff.
0
badea_andreiAuthor Commented:
But I do link some library for floating-point support - the library included with BC 3.1 for DOS. This code works perfectly

  #include <stdio.h>

  void main(void)
  {
    double a, b;

    scanf("%lf", &a);
    scanf("%lf", &b);

    printf("A + B = %lf", a + b);
  }

0
zebadaCommented:
I presume the sizeof syntax is correct, in your actual code. The example you give is missing the '(' character immediately after sizeof.
Not that it would cause the problem you say.

Is it worth trying the whole thing with float instead of double just to see....?
Although if the second piece of code works then the first should as well.

Compiler bug?

0
INTRODUCING: WatchGuard's New MFA Solution

WatchGuard is proud to announce the launch of AuthPoint, a powerful, yet simple, Cloud-based MFA service designed to eliminate the vulnerabilities that put your data, systems, and users at risk.

badea_andreiAuthor Commented:
Regarding the missing '('... I edited the code when I pasted it into the question because I wanted the rows to be shorter that the edit box.

I changed all doubles to floats, but this didn't help, it ends with the same error.
0
KangaRooCommented:
The error you mentioned often turns up with printf and scanf and usually (in my experience :( comes from the wrong argument(type)s in printf. But you seem to do being everything correct
0
zultiCommented:
surprizingly when i added the following line every thing works fine.
i'm not sure why !!!

    for (i = 0; i < 2; i++)
    {
      for (j = 0; j < 2; j++)
      {
        printf("Input [%d][%d]: ",
          i, j);

        array[i][j] = 0.0 ; /* this is the added line */

        scanf("%lf", &array[i][j]);
      }
    }
  }
0
AlexVirochovskyCommented:
Same (or ~ same) problem was discussed
before 1/2 year. Reason of error:
float array/variable is a part of
some structure and scanf for this variable produce error(in BC/VC).
Solution : or make variable off
this structure, or use some intermediate
variable (as you make).

0
kamareyCommented:
I have tested your program, and it work in two cases:
1. Instead scanf() I put function from C++: cin >> array[i][j];
   with function it work;
2. I defined array as static variable:
   double array[2][2];
   and it work with scanf().

But I also can't understand why your program doesn't work without it.
0
badea_andreiAuthor Commented:
Comment for zulti:
I tried adding the line as you say and compiling the program under BC 3.1 for DOS, but it didn't work.

0
deightonprogCommented:
double *dTemp;
..
..
..
..
  for (j = 0; j < 2; j++)
                       {
                         printf("Input [%d][%d]: ",
                           i, j);

                         array[i][j] = 0.0 ; /* this is the added line */

                         scanf("%lf", dTemp);
                         array[i][j] = *dTemp;
                       }
0
badea_andreiAuthor Commented:
For Deighton: thanks for your sugestion, but I don't want to use a temporary variable. I know it would work, and I actually use it in the final version of the program that I took my sample code from, but I want to know why it doesn't work the simplest way - without a temp variable.
0
KangaRooCommented:
deighton is not using a temporary variabe, he is initializing an existing one (from the array) to 0.0
If this works one is driven to believe that the scanf argument must point to valid doubles.
0
AlexVirochovskyCommented:
>>but I want to know why it doesn't work the simplest way - without a temp variable.
I think, that it is impossible:
only man, that wrote scanf , know
it, by he/she 'll not tell.
Best solution : don't use scanf,
as Kamarey write:
cin >> array[i][j];

0
abk102299Commented:
> But I do link some library for
> floating-point support - the library
> included with BC 3.1 for DOS. This > > code works perfectly

>   #include <stdio.h>

>   void main(void)
>   {
>     double a, b;

>     scanf("%lf", &a);
>     scanf("%lf", &b);

>     printf("A + B = %lf", a + b);
>   }

Yes. But the linker is too smart. :)
It strips out floating-point support because you don't use floating-point values explicitly ( pointers only ).
For example, VC6 generates the following
for your code:

> C Run-Time Error R6002
> floating-point support not loaded

> The program needs the floating-point
> library, but the library was not
> linked to the program.

> One of the following may
> have occurred:

[...]

> A format string for a printf or scanf > function contained a floating-point
> format specification, and the program
> did not contain any floating-point
> values or variables.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KangaRooCommented:
That is nasty. There may be a project setting to force FP support.
0
badea_andreiAuthor Commented:
Abk, YOU WERE RIGHT! I added some code like

  scanf("%lf", &x);

at the beginning of the program and it ran. Thanx very much and the points are yours. I have only one more question: do you know how do I force the linker to link the FP libs without adding that useless scanf at the beginning, in either BC 3.1 for DOS or MS VC++ 5.0?
0
abk102299Commented:
Sorry, but I have no a good answer for this one. For example, the following

#include <math.h>
const double fzero = fabs(0);

would force the FP libs linked.
Or hundreds of other similar tricks...
As for regular compiler/linker BC|MSVC options I don't see any. But this question is rather academic than practical because usually FP are used in mach more extensive way than just transfer from input to output. I think your sample is rather exception and IMHO for such exceptions some tricks may be satisfactory used.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.