# Thread: Finding a smaller array within a larger 2D array

1. ## Finding a smaller array within a larger 2D array

I have this code to try and locate a small array within a big array but it seems to crash on the first if statement. Both arrays are dynamically allocated from files and point to a strcuture.
Code:
```for (i=0;i<head[1].width-head[2].width;i++) //Looks for possible start postions
{
int wally = 0;
//printf("%d ", i);
for(j=0;j<head[1].height-head[2].height;j++)
{
wally = 1; //Tests if query array is found in main input array
for(qi=0; qi<head[2].height; qi++) //Looks through smaller array for match
{
for(qj=0;qj<head[2].width;qj++)
{
if ((RGBArray[i+qi][j+qj].red != QueryArray[qi][qj].red) || (RGBArray[i+qi][j+qj].green != QueryArray[qi][qj].green)
|| (RGBArray[i+qi][j+qj].blue != QueryArray[qi][qj].blue))
{
wally = 0;
break;
}
}

if(!wally)
break;

}
if(wally)
{
identify_y = j; //Stores j values that match in variable
identify_x = i; //Stores i values that match in variable
printf("%d ", i);
printf("%d ", j);
break;
}
}
}```

2. Maybe you should set a breakpoint on line 12 and inspect your arrays.
Maybe one of your pointers or both are invalid or you access them out of range.

3. Well it appears to run fine when I break before the first if statement but for ((RGBArray[i+qi][j+qj].red != QueryArray[qi][qj].red) || (RGBArray[i+qi][j+qj].green != QueryArray[qi][qj].green || (RGBArray[i+qi][j+qj].blue != QueryArray[qi][qj].blue)) i+qi is the widths of the arrays added together for example but I'm not accessing that really just checking it? And I'm not sure of another way to check if the array isn't in the big one.

4. Though it still crashes even if I change it to i-qi

5. Code:
`for(qj=0;qj<head[2].width;qj++)`
Code:
`for(j=0;j<head[1].height-head[2].height;j++)`
In one place, j seems to be related to width in the other height!

Which is it?

Tim S.

Popular pages Recent additions