sagen@nucthy.physics.orst.edu (Milt Sagen) (03/05/89)
If I compile the following code segment with MPW-C 3.0 using 'c -c file.c'
no errors are reported. However, when I compile it with LSC 3.0 it
reports an error because the array is unindexed. If I put in 6 as the
index then LSC reports an error for trying to initialize an automatic
array.
According to K&R (1st edition) automatic arrays cannot be initialized.
What does K&R's 2nd edition and the ANSI standard say about this?
Is this a bug in the MPW C compiler? Or is Apple following the ANSI
standard on this, while THINK Technologies follows K&R?
I couldn't find anything in the MPW-C manual about initializing automatic
arrays.
One last thing before I show you the code fragment: MPW-C leaves a .o
file, of length 0K, in the directory when I compile with the -c option (no
code generation option). Is this a bug, feature, oversight, or what?
#ifdef THINK_C
#define real double
#else
#define real extended
#endif
real XFactorial( x )
real x ;
{
real coef[] = ( 76.18009173, -86.50532033, 24.01409822
-1.231739516, 0.120858003e-2, 0.536382e-5 ) ;
:
:
:
}
Milt Sagen Internet: sagen@nucthy.physics.orst.edu
Department of Physics
Oregon State University
Corvallis, OR 97331 Tele: (503) 754-4631
oster@dewey.soe.berkeley.edu (David Phillip Oster) (03/06/89)
In article <9226@orstcs.CS.ORST.EDU> sagen@nucthy.physics.orst.edu (Milt Sagen) writes: _>#ifdef THINK_C _>#define real double _>#else _>#define real extended _>#endif _> _>real XFactorial( x ) _> real x ; _>{ _> real coef[] = ( 76.18009173, -86.50532033, 24.01409822 _> -1.231739516, 0.120858003e-2, 0.536382e-5 ) ; _>} this program has numerous errors, and certainly doesn't do what the author intended. Notice that Milt uses parentheses to delimit his list of constants: That makes the whole thing be a constant expression that returns its last component as its value. So, what he has is equivalent to real coef[1] = 0.536382e-5; Note further that even if he had used squiggly braces, there is a comma missing at the end of the first line, so he will get a 5 element array of which coef[2] == 24.01409822 - 1.231739516; The following version version is more correct: real XFactorial( x )real x;{ static real coef[] = { 76.18009173, -86.50532033, 24.01409822, -1.231739516, 0.120858003e-2, 0.536382e-5 } ; } This version is correct if, as is likely, coef will not change at runtime. I am surprised that MPW C passed the original without complaint. I like compilers to point out errors in my code. --- David Phillip Oster --"When we replace the mouse with a pen, Arpa: oster@dewey.soe.berkeley.edu --3 button mouse fans will need saxophone Uucp: {uwvax,decvax}!ucbvax!oster%dewey.soe.berkeley.edu --lessons." - Gasee
tim@hoptoad.uucp (Tim Maroney) (03/06/89)
In article <9226@orstcs.CS.ORST.EDU> sagen@nucthy.physics.orst.edu (Milt Sagen) writes: >If I compile the following code segment with MPW-C 3.0 using 'c -c file.c' >no errors are reported. However, when I compile it with LSC 3.0 it >reports an error because the array is unindexed. If I put in 6 as the >index then LSC reports an error for trying to initialize an automatic >array. > >real XFactorial( x ) > real x ; >{ > real coef[] = ( 76.18009173, -86.50532033, 24.01409822 > -1.231739516, 0.120858003e-2, 0.536382e-5 ) ; > : > : > : >} Parentheses? What are those doing there? You use curly braces to initialize aggregates. Because of the way the comma operator works, your initializer is a real number with the value 0.536382e-5. Any compiler that lets you assign that to an array/pointer without a type cast is confused. -- Tim Maroney, Consultant, Eclectic Software, sun!hoptoad!tim "When the writer becomes the center of his attention, he becomes a nudnik. And a nudnik who believes he's profound is even worse than just a plain nudnik." -- Isaac Bashevis Singer
sagen@nucthy.physics.orst.edu (Milt Sagen) (03/06/89)
In article <28295@ucbvax.BERKELEY.EDU> oster@dewey.soe.berkeley.edu.UUCP (David Phillip Oster) writes: > >this program has numerous errors, and certainly doesn't do what the >author intended. Notice that Milt uses parentheses to delimit his list of >constants: That makes the whole thing be a constant expression that >returns its last component as its value. So, what he has is equivalent to >real coef[1] = 0.536382e-5; Sorry about that, I think I was programming in Caspal (a mix of C and Pascal), at the time. >Note further that even if he had used >squiggly braces, there is a comma missing at the end of the first line, so >he will get a 5 element array of which coef[2] == 24.01409822 - 1.231739516; Yea, I caught the missing ',' right after I posted... >I am surprised that MPW C passed the original without complaint. I like >compilers to point out errors in my code. I was surprised at first, now I'm shocked. >--- David Phillip Oster --"When we replace the mouse with a pen, Thanks David. BTW: Have you upgraded the calender DA beyond 1.8? Milt Sagen Internet: sagen@nucthy.physics.orst.edu Department of Physics Oregon State University Corvallis, OR 97331 Tele: (503) 754-4631
sagen@nucthy.physics.orst.edu (Milt Sagen) (03/06/89)
JFTHOI, I went back and changed the code so it read: static real coef[] = ( 76.1, -86.5, 24.0, -1.2, 0.12e-2, -0.5e-5 ) ; where I have shortened the numbers for this note only, and yet retained the mistake, pointed out by David Oster, with regard to the use of '(' and ')' as opposed to '{' and '}'. This MPW-C caught and issued the error message 'Constant expression expected'. Milt Sagen Internet: sagen@nucthy.physics.orst.edu Department of Physics Oregon State University Corvallis, OR 97331 Tele: (503) 754-4631