title: Error when storing and calling values from a dynamic array: program output not as expected
body: <p>I have written a program that calculates the numbers 1-8 in binary (each as four bits), stores them in an array and then prints the resulting string. The output should be: 0000,0001,0010,0011,0100…etc. However my program is printing: 0000,0001,0000,0001 and I can't seem to find the problem so any help would be appreciated.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;math.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main()
  {
        int no,i=0,j,val,small[3],l;
        int *d;
        d = malloc(sizeof(int)*500);
        l=0;
        if (d != NULL) {
        printf("1\n");
        } else {
        printf("0\n");
        }
  
        for(j=0;j&lt;4;j++) //sets initial 0000 combination
        {
          d[j]=0;
        }
  
        for(no=1;no&lt;8;no++)  //loop for numbers 1-8
        {
          val=no;
          while(val&gt;0)         //decimal to binary conversion
          {
  
             small[i]=val%2;
             i++;
             val=val/2;
             l++;
          }
          i=0;
            printf("%d\n",l);
          if(l&lt;4)                     //creating 4 bit binary number
          {
              if(l=1)
              {
                  small[3]=small[0];
              }
              else
              {
                  for(j=(l-1);j&gt;=0;j--)
                  {
                      small[(j+(4-l))]=small[j];
                  }
              }
              for(j=0;j&lt;(4-l);j++)
              {
                  small[j]=0;
              }
          }
          l=0;
          for(j=0;j&lt;4;j++)            //storing binary number in dynamic array
          {
              d[(4*no)+j]=small[j];
              small[j]=0;
          }
        }
  
        for(j=0;j&lt;(4*(no-1))+4;j++) //printing array
        {
              printf("%d",d[j]);
        }
  
   }
  </code></pre>