title: Array of structures in C++:assignment of values to each structure in array
body: <p>I have declared a structure st below with a struct variable arr[], an array of structs.
  Im trying to assign the value 1 to the 'num' variable, and values 1 to 10 to 'val' variable of the first 10 locations of array arr[]. And value 2 to 'num' and values 1 to 10 to 'val' of the next 10 locations. But when i traced the code, it won't assign values to the respective num and val of the same array location. If i wanted to assign num=1 and val=4 to the 4th structure it would assign num=1 to val of 3rd structure and val=4 to num of 4th structure.</p>
  
  <p>My query is not about array indices.</p>
  
  <p>The problem is: </p>
  
  <p>If i wrote the statements </p>
  
  <p>arr[2].num=1;
  arr[2].val=2;</p>
  
  <p>({num,val})</p>
  
  <p>The expected result is: arr[2]={1,2} </p>
  
  <p>But the actual result is: 
  arr[1]={num,1}
  arr[2]={2,val}</p>
  
  <pre><code>#include&lt;iostream.h&gt;
  #include&lt;conio.h&gt;
  
  
  class abc
  {
  
   public:
  
   struct st
   {
    int num;
    int val;
   };
  
   st arr[21];
  
   void funct();
  
  };
  
  void abc::funct()
  {
   int i,j,k=1;
  
   for(i=1;i&lt;=2;i++)
   {
    for(j=1;j&lt;=10;j++)
    {
     arr[k].num=i;
     arr[k].val=j;
     k++;
    }
   }
  
  }
  
  
  int main()
  {
   abc z;
  
   z.funct();
  
   return 0;
  }
  </code></pre>