title: How can I putting dynamically string into array using C
body: <p>J have below a code which display the name (this is char[]/string) of directories or files in given directory. I want to put this names dynamically to array. In print table1 displays correct the values(name of directories or files) which are iterate. But in other print table3 displays incorrect values. Could anybody help me with that? Is this problem with memory or something else?     </p>
  
  <pre><code>char** do_ls( char dirname[] ){
  DIR *dir_ptr;
  struct dirent *direntp;
  char **strArray;
  int count=0;
  strArray = (char**)malloc(1 * sizeof(char*));
  strArray[0] = NULL;
  
  if ( ( dir_ptr = opendir( dirname ) ) == NULL )
      fprintf(stderr,"ls1: cannot open %s\n", dirname);
  else {
      while ( ( direntp = readdir( dir_ptr ) ) != NULL ) {
          if(strchr(direntp-&gt;d_name, '.') == NULL) {
  
              strArray[count] = (char*)direntp-&gt;d_name;
              printf("\nTable1: %s \n", strArray[count]);
              count++;
              strArray = (char **)realloc(strArray, sizeof(char *) * (count + 1));
              printf("\nCount: %d \n", count);
  
          }
      }
      strArray[count] = NULL;  /* Terminate the array */
  printf("\nTable3: %s \n", strArray[0]);
  printf("\nTable3: %s \n", strArray[1]);
  printf("\nTable3: %s \n", strArray[2]);
  printf("\nTable4: %s \n", strArray[strlen(strArray)-1]);
  printf("\nIndex: %d \n", strlen(strArray)-1);
  
      closedir(dir_ptr);
  }
  
  
  
  return strArray;
  </code></pre>
  
  <p>}</p>