title: Unable to enter while loop:"Segmentation fault"
body: <p>I don't know why I am getting segmentation fault when trying to do while(mapArray[currX][currY]!='t')
  Every things is working fine as far as I can run.</p>
  
  <pre><code>#include&lt;iostream
  #include&lt;vector&gt;
  #include&lt;queue&gt;
  using namespace std;
  
  struct array{
  private:
  char mapArray[10][10];
  char tempChar;
  queue&lt;int&gt; xComponent;
  queue&lt;int&gt; yComponent;
  vector&lt;vector&lt;char&gt;&gt; visited;
  vector&lt;char&gt; answer;
  int xtarget=1+rand() % 10;
  int ytarget=1+rand() % 10;
  
  void inputMap();
  void moveRight(int xC,int yC);
  void moveDown(int xC,int yC);
  void moveLeft(int xC,int yC);
  void moveUp(int xC,int yC);
  void move();
  bool isOk(int xC,int yC);
  void showPath();
  public:
  void run();
  };
  
  void array::moveRight(int xC,int yC){
  xComponent.push(xC);
  yComponent.push(yC);
  }
  
  void array::moveDown(int xC,int yC){
  xComponent.push(xC);
  yComponent.push(yC);
  }
  
  void array::moveLeft(int xC,int yC){
  xComponent.push(xC);
  yComponent.push(yC);
  }
  
  void array::moveUp(int xC,int yC){
  xComponent.push(xC);
  yComponent.push(yC);
  }
  
  bool array::isOk(int xC,int yC){
  return ((visited[xC][yC]!='v') &amp;&amp; (mapArray[xC][yC]!='t'));
  if(visited[xC][yC]=='v')
  return false;
  else
  if(mapArray[xC][xC]=='x')
  return false;
  else
  return true;
  }
  
  void array::inputMap(){
      cout&lt;&lt;"Values of map"&lt;&lt;endl;
      //Using random numbers
      /*
      for(int i=0;i&lt;10;i++){
          for(int j=0;j&lt;10;j++){
              cout&lt;&lt;"Input value for i:"&lt;&lt;i&lt;&lt;" j:"&lt;&lt;j&lt;&lt;endl;
              cin&gt;&gt;mapArray[i][j];
          }
      }
      */
      for(int i=0;i&lt;10;i++){
          for(int j=0;j&lt;10;j++){
              int t=rand() % 9;
              char chA='0' + t;
              //cout&lt;&lt;"T:"&lt;&lt;t&lt;&lt;endl;
              //cout&lt;&lt;"CHA:"&lt;&lt;chA&lt;&lt;endl;
              mapArray[i][j]=chA;
              if(i==xtarget&amp;&amp;j==ytarget){
                  mapArray[i][j]='t';
              }
              cout&lt;&lt;"i:"&lt;&lt;i&lt;&lt;" j:"&lt;&lt;j&lt;&lt;" "&lt;&lt;mapArray[i][j]&lt;&lt;endl;;
          }
      }
  }
  
  void array::move(){
  int currX=0,currY=0;
  xComponent.push(0);
  yComponent.push(0);
  cout&lt;&lt;"Debugging move point 1"&lt;&lt;endl;
  
  cout&lt;&lt;"Debugging move point 2"&lt;&lt;endl;
  while(mapArray[currX][currY]!='t'){
  cout&lt;&lt;"Debugging while:"&lt;&lt;currX+1&lt;&lt;" ";
  if((isOk(currX+1,currY) &amp;&amp; (currX+1!=10) &amp;&amp; (visited[currX+1][currY]!='v')))
  moveRight(currX+1,currY);
  
  if((isOk(currX,currY+1) &amp;&amp; (currY+1!=10) &amp;&amp; (visited[currX+1][currY]!='v')))
  moveDown(currX,currY+1);
  
  if((isOk(currX-1,currY) &amp;&amp; (currX-1!=-1) &amp;&amp; (visited[currX-1][currY]!='v')))
  moveLeft(currX-1,currY);
  
  if((isOk(currX,currY-1) &amp;&amp; (currY-1!=-1) &amp;&amp; (visited[currX][currY-1]!='v')))
  moveUp(currX,currY-1);
  
  if( (!xComponent.empty()) &amp;&amp; (!yComponent.empty()) ){
      currX=xComponent.front();
      currY=yComponent.front();
      xComponent.pop();
      yComponent.pop();
      visited[currX][currY]='v';
      tempChar=mapArray[currX][currY];
      answer.push_back(tempChar);
  }
  
  }
  
  cout&lt;&lt;"Debugging move point 3"&lt;&lt;endl;
  
  /*
  xComponent.push(currX);
  yComponent.push(currY);
  visited[currX][currY]='v';
  tempChar=mapArray[currX][currY];
  answer.push_back(tempChar);
  */
  }
  
  void array::run(){
      cout&lt;&lt;"Inside run()"&lt;&lt;endl;
      cout&lt;&lt;"Target Block x:"&lt;&lt;xtarget&lt;&lt;" y:"&lt;&lt;ytarget&lt;&lt;endl;
      inputMap();
      cout&lt;&lt;"inputMap() done"&lt;&lt;endl;
      move();
      cout&lt;&lt;"move() done"&lt;&lt;endl;
      showPath();
      cout&lt;&lt;"showPath() done"&lt;&lt;endl;
  }
  
  void array::showPath(){
      cout&lt;&lt;"Answer:";
      for(int i=0;i&lt;answer.size();i++){
          cout&lt;&lt;answer[i]&lt;&lt;" ";
      }
  }
  
  int main(){
  cout&lt;&lt;"Hello World"&lt;&lt;endl;
  array A;
  A.run();
  return 0;
  }
  </code></pre>
  
  <p>I was trying to do bfs on array but I am getting segmentation fault each time I try to access 0,0 element of mapArray</p>
  
  <p>I read on internet and find that when we try to access location which is out of scope we get segmentation fault but in our case it's not.</p>