Turbo C variable changing?

Pete Halverson halvers at altair.crd.ge.com
Thu Aug 16 00:52:42 AEST 1990


In article <90227.090858JJL101 at 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 at crd.ge.com 
GE Corporate R&D Center	                      UUCP: uunet!crd.ge.com!halverson
Schenectady, NY                     "Money for nuthin' and your MIPS for free"



More information about the Comp.lang.c mailing list