title: Why can't I save a pointer to a struct to an index of a pointer to an array of pointers?
body: <p>I've done something similar to this in the past so I'm not sure why when I try to print my pointer to an array of pointers I get a bunch of (null)'s as output.  This is the code I'm speaking of near the bottom of the file: </p>
  
  <pre><code>int z = 0;
  while (z &lt; 9) {
      printf("%s ", allLines[z]-&gt;username);
      z++;
  } 
  </code></pre>
  
  <p>What I'm trying to do is get a directory (either relative or absolute) from the user then change to that directory and open all the text files inside that directory so that I can extract each line from them and store each line inside a records struct.  Please see my code below: </p>
  
  <pre><code>#define _GNU_SOURCE
  #define MAXLINE 256
  #define MAXPATHLENGTH 1024
  #define MAXRECORDS 10000
  #include &lt;fcntl.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;dirent.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;pthread.h&gt;
  
  typedef struct records {
      char* username;
      char* password;
      char* bloodType;
      char* domainName;
      char* index;
  } records;
  
  int main(int argc, char** argv) {
      if(argc != 2) {
          printf("You must provide 2 arguments: &lt;./filename&gt; &lt;directory&gt;\n");
          return -1;
      }
  
      char* path = malloc(MAXPATHLENGTH * sizeof(char));
      char* cwd = malloc(MAXPATHLENGTH * sizeof(char));
      FILE* sortedFile;
      FILE* dirEntry;
      DIR* dirp; //pointer to a directory stream  
      struct dirent* dirstructp; //pointer to a dirent structure 
      struct stat buffer;
      records** allLines = malloc(MAXRECORDS * sizeof(records));
      int linesInFile[MAXRECORDS];
      records** files = calloc(MAXRECORDS, sizeof(records)); //pointer to an array of pointers (each of which point to a records struct)
      int totalFiles;
  
      if ((sortedFile = fopen("sorted.yay", "w+")) == NULL) {
          fprintf(stderr, "%s\n", strerror(errno));
          printf("fopen");
          exit(errno);
      }
  
      //changes the current working directory of the calling process to the directory specified
      if((chdir(argv[1])) == -1) {
          fprintf(stderr, "%s\n", strerror(errno));
          exit(errno);
      }
  
      /*copies the pathname of the current working directory to the array pointed to by cwd, which is of length MAXPATHLENGTH*/
      if((path = getcwd(cwd, MAXPATHLENGTH)) == NULL) {
          fprintf(stderr, "%s\n", strerror(errno));
          exit(errno);
      }
  
      //returns a pointer to the directory stream if successful 
      if((dirp = opendir(path)) == NULL) {
          fprintf(stderr, "%s\n", strerror(errno));
          printf("opendir");
          exit(errno);
      }
  
      int i = 0;
      int j = 0;
      int l = 0;
      while((dirstructp = readdir(dirp)) != NULL) {
  
          if ((strncmp(dirstructp-&gt;d_name, ".", strlen(".")) != 0) &amp;&amp; (strncmp(dirstructp-&gt;d_name, "..", strlen("..")) != 0)) {
  
              if(lstat(dirstructp-&gt;d_name, &amp;buffer) == -1) {
                  fprintf(stderr, "%s\n", strerror(errno));
                  printf("Bob Saget");
                  exit(errno);
              }
  
              if(S_ISREG(buffer.st_mode)) {
  
                  if ((dirEntry = fopen(dirstructp-&gt;d_name, "r")) == NULL) {
                      fprintf(stderr, "%s\n", strerror(errno));
                      printf("fopen2");
                      exit(errno);
                  }   
  
                  char* buf = malloc(MAXLINE * sizeof(char));
                  while(fgets(buf, MAXLINE, dirEntry)) {                            
                      records* line = malloc(sizeof(records));       
                      int k = 0;         
                      while(k &lt; 5) {                            
                          char* token = malloc(MAXLINE * sizeof(char));                
                          token = strsep(&amp;buf,",");                                               
                          switch(k) {                                            
                              case 0:                                                           
                                  line-&gt;username = malloc(MAXLINE * sizeof(char));                                   
                                  strncpy(line-&gt;username, token, strlen(token));                           
                                  break;                                             
                              case 1:                                              
                                  line-&gt;password = malloc(MAXLINE * sizeof(char));            
                                  strncpy(line-&gt;password, token, strlen(token));                          
                                  break;                                             
                              case 2:                                              
                                  line-&gt;bloodType = malloc(MAXLINE * sizeof(char));            
                                  strncpy(line-&gt;bloodType, token, strlen(token));                      
                                  break;                                             
                              case 3:                                                
                                  line-&gt;domainName = malloc(MAXLINE * sizeof(char));           
                                  strncpy(line-&gt;domainName, token, strlen(token));                
                                  break;                                              
                              case 4:    
                                  line-&gt;index = malloc(MAXLINE * sizeof(char));
                                  strncpy(line-&gt;index, token, strlen(token));         
                                  break;                                             
                          }
                          k++;                                                      
                      }                                                         
                      allLines[l] = line;         
                      l++;
                      free(line);                                                                  
                      free(buf);                
                      buf = malloc(MAXLINE * sizeof(char));                      
                  }                      
                  linesInFile[j] = i;
                  i = 0;    
                  files[j] = allLines;  
                  j++;         
                  totalFiles = j;
                  free(buf);
              } 
          }
      }
      int z = 0;
      while (z &lt; 9) {
          printf("%s ", allLines[z]-&gt;username);
          z++;
      }   
      return 0;
  }   
  </code></pre>