[comp.lang.c] Turbo C variable changing?

halvers@altair.crd.ge.com (Pete Halverson) (08/15/90)

In article <90227.090858JJL101@psuvm.psu.edu> J.J. Lehett writes:
>
>      I have integer variables declared as index, and temp[2],
>
>    well when I start into a loop such as :
>
>      for (index = 1; index <= 2; index++)
>        { temp[index] =  u[index] + v[index];
>        }
>
>      that the statement line also changes the value of index.

Almost certainly caused by the out-of-bounds array reference:

   1) arrays in C always begin at element 0

   2) temp is defined to be 2 elements long

   3) the iteration runs from 1 to 2 (should be from 0 to 1)

   4) assigning something to temp[2] will cause strange behavior, in this
      case modifying the value of index due (most likely) to the way the
      compiler arranged the variables on the stack.

Changing this to

  for (index = 0; index < 2; index++)
  {
     temp[index] =  u[index] + v[index];
  }

should eliminate the problem.
--
===============================================================================
Pete Halverson                        		    INET: halverson@crd.ge.com 
GE Corporate R&D Center	                      UUCP: uunet!crd.ge.com!halverson
Schenectady, NY                     "Money for nuthin' and your MIPS for free"