[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Problem with dynamic arrays and scanf

Posted on 1999-11-08
17
Medium Priority
?
573 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:badea_andrei
  • 5
  • 3
  • 2
  • +5
17 Comments
 
LVL 6

Expert Comment

by:zebada
ID: 2192611
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
 

Author Comment

by:badea_andrei
ID: 2192682
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
 
LVL 6

Expert Comment

by:zebada
ID: 2192936
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:badea_andrei
ID: 2193315
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2193427
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
 
LVL 2

Expert Comment

by:zulti
ID: 2193697
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
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2194149
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
 
LVL 2

Expert Comment

by:kamarey
ID: 2195019
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
 

Author Comment

by:badea_andrei
ID: 2195593
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
 
LVL 18

Expert Comment

by:deighton
ID: 2196287
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
 

Author Comment

by:badea_andrei
ID: 2196672
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2197186
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
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2197230
>>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
 
LVL 1

Accepted Solution

by:
abk102299 earned 400 total points
ID: 2198487
> 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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2200020
That is nasty. There may be a project setting to force FP support.
0
 

Author Comment

by:badea_andrei
ID: 2200520
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
 
LVL 1

Expert Comment

by:abk102299
ID: 2200796
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

Featured Post

Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question