Home > Net >  Why looping through array gives non existent value?
Why looping through array gives non existent value?

Time:02-01

I have 2 multidimensional arrays filled with x and y coordinates. Im comparing both arrays to find a match in nested loop like this

int order;
order = 0;
float c[4][2];
for (int i = 0;i <= sizeofarrB; i   ) {
   for (int j = 0;j <= sizeofarrA; j   ) {
        if (a[j][0] == b[i][0] && a[j][1] == b[i][1]){
        printf("testx = %f y = %f\n " , a[j][0], a[j][1]);
        printf("testx = %f y = %f\n", b[i][0], b[i][1]);
            c[order][0] = b[i][0];
            c[order][1] = b[i][1];
            order  ;
        }

   }
}

first array

these are values of first array

second array

these are values of second array

and these are the values that i get from running that nested loop final values

i have no idea how to fix the code to get correct values

CodePudding user response:

You use i <= sizeofarrB as a loop condition, but you should use i < sizeofarrB, as an array of size 4 will have indices from 0 to 3.

You are reading out of the memory area that belongs to the array, a typical way of corrupting memory in C.

  •  Tags:  
  • Related