title: pointer array with strings using malloc and buffer in C
body: <p>I am stuck in how to fill a pointer array with strings using malloc. In debug i see that when i fill the 1st pointer of array with a string, when its about to go to next pointer in the array it pass the next string in both 1st and second element... seems like when i use <code>ptr[i]=buff;</code> the ptr keeps showing in the buff array. </p>
  
  <pre><code>    #include&lt;stdlib.h&gt;
      #include&lt;string.h&gt;
      #define size 2       //array of 2 pointers
      int main()
  {
      int i;
      char *ptr[size];
      char buff[80];
  
      for (i=0;i&lt;size;i++)
      {
          memset(buff, 0, sizeof(char) * 80);
          printf("Enter name:\n");fflush(stdout);
          scanf("%s",buff);
  
          ptr[i]=(char*)malloc(strlen(buff));
          //ptr[i]=buff;                        //that was the mistake
          strncpy(ptr[i], buff, strlen(buff));    //-&gt;correct answer!
          printf("length %d\n",strlen(buff));
      }
      for (i=0;i&lt;size;i++)
      {
          printf("prt[%d]=%s\n",i,ptr[i]);fflush(stdout);
      }
      for (i=0;i&lt;size;i++)
      {
          free(ptr[i]);
      }
      return 0;
  }
  </code></pre>
  
  <p>Another weird question that i have has to do with the length of the arrays in general. When an array is declared for example <code>a[10]</code> the pointer a points to the first element of the array. What i do not understand is where the length is being stored!? is it the previous memory address of the pointer a? Is it before? Or does it have to do with the compiler only?
  Thanks. i hope that wasnt too much i asked. :)</p>