prototyping (oh no! not again??)
brian_helterline
brianh at hpcvia.CV.HP.COM
Fri Nov 30 03:01:32 AEST 1990
davis at pacific.mps.ohio-state.edu ("John E. Davis") writes:
:In article <1990Nov28.010539.22412 at druid.uucp> darcy at druid.uucp (D'Arcy J.M. Cain) writes:
: In <DAVIS.90Nov26144044 at pacific.mps.ohio-state.edu> John E. Davis writes:
: > double trace(double **matrix, int dim)
: > {
: > int i;
: > double t;
: >
: > t = 0.0;
: > i = 0;
: > while(i++ < dim) t = t + matrix[i][i];
: My first reaction was that you wind up accessing matrix[10][10] so you are
: accessing an area outside of the array. you should use:
: for (i = 0; i < dim; i++) ...
: but that doesn't explain why it works when you use:
: > double trace(double matrix[10][10],int n)
: unless that changes the program just enough that the area following
: the array is now part of your data space. If so that is just bad news
: somewhere else anyway. Try using the for construct.
:This is not the function that I really had in mind. I 'invented' this one as
:I was composing the article as a quick example the make my point. I guess I
:should write:
: i = 0; while(i < dim) t = t + matrix[i][i++];
^^^^^^^
Wrong again! There is no guarantee that this will evaluate to
what you want. If i=5, you could end up with matrix[6][5].
Using a variable more than once in an expression with a post-
increment is *undefined behavior*.
What do you have against a for(i=0; i<dim; ++i ) loop?
:I noticed this after I posted it.
[rest deleted]
More information about the Comp.lang.c
mailing list