scanf() w/ 2D-array address offsets: HELP! 
Author Message
 scanf() w/ 2D-array address offsets: HELP!

  I don't give up too easily, but this one has got me ripping my hair out.
According to every C book I've checked, my syntax is correct (although
none cover the combined particulars of this situation).
  I'm using Turbo C 2.0 on a 386 MS-DOS system, trying to scan
double-precision floats into a 2-D array via a function to which I pass
the address of the array.  The specification is that I treat the array as
2-D, by acting upon its elements with a nested loop.  The challenge that I
have inflicted upon myself is to adhere strictly to pointer notation.
  Here are the relevant parts (verified by isolation):

#include <stdio.h>
void store2d(double (*)[5], int);

int main(void)
{
  static double values[3][5];
  store2d(values, 3);
  /*  ...  */
  return 0;

Quote:
}

void store2d(double (*ar)[5], int size)
{
  int row, col;
  for (row=0; row<size; row++, ar++)
    for (col=0; col<5; col++) {
      printf("Enter a type double number:  ");
      scanf("%lf", *ar+col);
    }

Quote:
}

  The code compiles without error or warning, but a run-time error message
appears directly after the prompt for the number:

scanf : floating point formats not linked
Abnormal program termination

  I am aware that this is the same message that occurs when what scanf() is
given is not a pointer.  I have double-checked the size and value of the
argument, its reference, and even its dereference (useless as it sounds) -
yet everything in the code seems to be the way it should.  The problem
does not occur when the array is simplified to one dimension.  When I
change the 2-D array to any simple data type other than floating point
types, the problem also disappears.
  I'm not looking for a quick-fix - I really want to learn what is
preventing me from running this thing the way I want it.  Am I going
overboard with the pointer notation?  Is there an undocumented bug in
scanf() that would explain this phenomenon?  Has anyone ever seen this before?

                            Obliged to Bearers of Solutions,

                             ------- Matt Rafferty -------



Wed, 27 Dec 1995 07:31:47 GMT  
 scanf() w/ 2D-array address offsets: HELP!

Quote:

>  I don't give up too easily, but this one has got me ripping my hair out.
>According to every C book I've checked, my syntax is correct (although
>none cover the combined particulars of this situation).
>  I'm using Turbo C 2.0 on a 386 MS-DOS system, trying to scan
>double-precision floats into a 2-D array via a function to which I pass
>the address of the array.  The specification is that I treat the array as
>2-D, by acting upon its elements with a nested loop.  The challenge that I
>have inflicted upon myself is to adhere strictly to pointer notation.
>  Here are the relevant parts (verified by isolation):

>#include <stdio.h>
>void store2d(double (*)[5], int);

>int main(void)
>{
>  static double values[3][5];
>  store2d(values, 3);
>  /*  ...  */
>  return 0;
>}

>void store2d(double (*ar)[5], int size)
>{
>  int row, col;
>  for (row=0; row<size; row++, ar++)
>    for (col=0; col<5; col++) {
>      printf("Enter a type double number:  ");
>      scanf("%lf", *ar+col);
>    }
>}

>  The code compiles without error or warning, but a run-time error message
>appears directly after the prompt for the number:

>scanf : floating point formats not linked
>Abnormal program termination

Your code looks fine. I tried it out here and added some code to print ot
the elements of values in main(). It worked fine. I don't know Turbo C but
the error suggests that there is some sort of floating point support you
need to link in with the code which you have overlooked. All I can suggest
is check the manual for this.

-----------------------------------------


-----------------------------------------



Wed, 27 Dec 1995 22:01:05 GMT  
 scanf() w/ 2D-array address offsets: HELP!

Quote:


>>  I don't give up too easily, but this one has got me ripping my hair out.
>>  I'm using Turbo C 2.0 on a 386 MS-DOS system, trying to scan
>>  The code compiles without error or warning, but a run-time error message
>>appears directly after the prompt for the number:

>>scanf : floating point formats not linked
>>Abnormal program termination

>Your code looks fine. I tried it out here and added some code to print ot
>the elements of values in main(). It worked fine. I don't know Turbo C but
>the error suggests that there is some sort of floating point support you
>need to link in with the code which you have overlooked. All I can suggest
>is check the manual for this.

There was a bug in the Turbo C 2.0 compiler, which was present in both
the IDE and command-line compiler.  I believe that the patch for it is
in:

Directory PD1:<MSDOS.TURBO-C>
TC2PAT2.ARC   B   29195  890915  Turbo C v2.0 patches from Borland (4/89)

However, I'm not sure.  I've since switched to DJGPP on my DOS box.

--

____ "Nuke the unborn gay whales" -- Never seen on a protest sign
\bi/ I have no time for petty theft, I have no time for sex,
 \/  But I have time for what I like, And that is what is best.



Thu, 28 Dec 1995 07:51:47 GMT  
 scanf() w/ 2D-array address offsets: HELP!

Quote:



>>>  I don't give up too easily, but this one has got me ripping my hair out.
>>>  I'm using Turbo C 2.0 on a 386 MS-DOS system, trying to scan
>>>  The code compiles without error or warning, but a run-time error message
>>>appears directly after the prompt for the number:

>>>scanf : floating point formats not linked
>>>Abnormal program termination

>There was a bug in the Turbo C 2.0 compiler, which was present in both
>the IDE and command-line compiler.  I believe that the patch for it is
>in:

>Directory PD1:<MSDOS.TURBO-C>
>TC2PAT2.ARC   B   29195  890915  Turbo C v2.0 patches from Borland (4/89)

No, this isn't a "Bug", its a "Feature" of all (so far as I know) Borland C
compilers.  ( a feature is any bug that has been documented ;-)  )

Borland has a tech note on the cure, which I will mail to the original
questioner.

The linker won't link the float support unless it can see some real float
operations.  Just using floats in scanf() is not sufficient.  I suppose since
any Real(tm) program that reads floats will also do some calculations on them,
the problem will not normally arise.  It will, howver, appear in little demo
programs that just read floats, but don't do any calculations.

Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight


TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)



Thu, 28 Dec 1995 05:26:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Which 2D array of 2D array addressing method?

2. 2d array and scanf

3. 2D array of pointers to 2D arrays

4. address of 2D array elements?

5. PMode Address -> Segment:Offset Address

6. Combine zero-offset with unit-offset arrays ?

7. Help manipulating 2D array using (not and)

8. Help manipulating 2D array

9. Help with 2D array, fixed 2nd dimension

10. Help : Pointer to 2D Array

11. nested loops/2d-arrays HELP (please)

12. Help manipulating 2D array using (not and)

 

 
Powered by phpBB® Forum Software