title: Probably a silly,often asked, mistake involving arrays in C
body: <p>I am very new to C and I am having problems with the following. I initialize the arrays:</p>
  
  <pre><code>double Cx[101];
  for(int i=0; i&lt;101; i++){
    Cx[i]=-5+i*0.1;
  }
  
  double Q[2][101];
  double y[101];
  </code></pre>
  
  <p>and I have the following functions outside the main method:</p>
  
  <pre><code>double InitHeight(double g, double dx, double x){
    return 3;
  }
  
  double InitMom(double g, double dx, double x){
   return 2;
  }
  
  double plainTopo(double x){
   return x*10;
  }
  </code></pre>
  
  <p>Now, in the main method I do the following:</p>
  
  <pre><code>double g=1;
  double dx=0.1;
  for(int i=0; i&lt;101; i++){
    Q[1][i] = InitHeight(g,dx,Cx[i]);
    Q[2][i] = InitMom(g,dx,Cx[i]);
    y[i] = plainTopo(Cx[i]);
  }
  </code></pre>
  
  <p>So, my problem is that the original values of the Cx array get modified. Here is a portion of the original Cx:</p>
  
  <pre><code>-5.000000
  -4.900000
  -4.800000
  -4.700000
  -4.600000
  -4.500000
  -4.400000
  ...
  </code></pre>
  
  <p>and after I run the the code this is what I get:</p>
  
  <pre><code>   Cx[]   Q[1][]   Q[2][]     y[]
  2.000000 3.000000 2.000000 20.000000
  2.000000 3.000000 2.000000 20.000000
  2.000000 3.000000 2.000000 20.000000
  2.000000 3.000000 2.000000 20.000000
  2.000000 3.000000 2.000000 20.000000
  2.000000 3.000000 2.000000 20.000000
  </code></pre>
  
  <p>I can't for the life of me see why the Cx array gets modified. I have tried to use pointers instead with the same results, also tried to do
      double x = Cx[i]
  in the loop and then pass x to the functions but again Cx gets change.</p>
  
  <p>Any help would be greatly appreciated it.</p>