Path: utzoo!utgpu!watserv1!watmath!att!linac!pacific.mps.ohio-state.edu!zaphod.mps.ohio-state.edu!sdd.hp.com!hp-pcd!hpcvia!brianh From: brianh@hpcvia.CV.HP.COM (brian_helterline) Newsgroups: comp.lang.c Subject: Re: prototyping (oh no! not again??) Message-ID: <31530031@hpcvia.CV.HP.COM> Date: 29 Nov 90 16:01:32 GMT References: Organization: Hewlett-Packard Co., Corvallis, Oregon Lines: 38 davis@pacific.mps.ohio-state.edu ("John E. Davis") writes: :In article <1990Nov28.010539.22412@druid.uucp> darcy@druid.uucp (D'Arcy J.M. Cain) writes: : In 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