c - program.exe stops working after compiling? -


i trying write code obtain patch/sub_window of array. wrote following code:

#include <stdio.h>  int patch(int a[5][5],int b[2][2],int r,int s) {  int i=0,j=0,k;  if(r<=(5-2) && s<=(5-2)){      for(r;r<(r+2);r++){         for(s;s<(s+2);s++)         {             k = a[r][s];             b[i][j] = k;              = i+1;             j = j+1;          }     } } else {printf("error!");}  return 0; }   int main() { int i,j,p,q; int y[2][2] = {0};  int x[5][5] = {{95,155,200,200,232}, {100,155,232,95,150}, {200,45,200,135,123}, {232,150,85,69,180}, {95,95,200,123,45} };  for(i=0;i<5;i++){     for(j=0;j<5;j++){         patch(x,y,i,j);         for(p=0;p<2;p++){             for(q=0;q<2;q++)             {                 printf("y[%d][%d] = %d\n",p,q,y[p][q]);              }         }     } }   return 0; } 

however, once run , compile code error stating program.exe has stopped working. how can fix ? need use dynamic memory allocation kind of process? or there simpler solution.

this line:

for(r;r<(r+2);r++) 

will run lot longer intend, causing buffer overflow. think mean save r+2 before starting loop, , compare against that.

also, increment i , j 4 times, access out bounds of b too. need re-think loop logic. perhaps should be:

for (i = 0 ; < 2; i++)     (j = 0; j < 2; j++)      {         b[i][j] = a[r+i][s+j];     } 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

c# - Unity IoC Lifetime per HttpRequest for UserStore -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -