title: Clueless: Infinite Loop in C
body: <p>i am working on a c implementation of <code>Eratostenes</code> method of finding prime numbers and i am running into a very weird error.</p>
  
  <pre><code>int* criba(int n){
  
      int *array;
      int i,j,k,valor;
      array = malloc(n*sizeof(int));
  
      for (i=0;i&lt;=n-2;i++){
          array[i]=i+2;
      }
  
      for (j=0;j&lt;n-2;j++){
          valor = array[j];
          for (k=1;valor*k+j&lt;n-2;k++){
  
              printf("%d \n",array[valor*k+j]);
  
              }
          }
      }
  
  
      return array;
  }
  </code></pre>
  
  <p>This works as intended, it creates an <code>n-2</code> sized array, with all the numbers from to 2 to n, then it checks every value and prints all of its multiples inside the array (Eratostenes's method seeks for all the multiples of a number starting from 2 and marks them in a <code>table</code>, then it goes to the next unmarked number, after that, all the unmarked number are prime numbers)</p>
  
  <p>The problem is, when i try to zero the numbers to "mark" them, i get stuck in a loop for no reason at all:</p>
  
  <pre><code>int* criba(int n){
  
      int *array;
      int i,j,k,valor;
      array = malloc(n*sizeof(int));
  
      for (i=0;i&lt;=n-2;i++){
          array[i]=i+2;
      }
  
      for (j=0;j&lt;n-2;j++){
          valor = array[j];
          for (k=1;valor*k+j&lt;n-2;k++){
              if (array[valor*k+j] != 0){
                  printf("%d \n",array[valor*k+j]);
                  array[valor*k+j] = 0;
              }
          }
      }
  
  
      return array;
  }
  </code></pre>
  
  <p>In fact, the only problem is <code>array[valor*k+j] = 0;</code>, if i remove that the program returns sucesfully, but with that line it gets stuck and i can't find why. Any help is greatly appreciated.</p>