title: Failing to access data from an array
body: <p>I am reading from a file (each line wolds 1 word) and putting each line into an array. However I'll get a segmentation fault whenever I try to access any element in the array. Any help on this is greatly appreciated.
  *update: added a while loop to grab the character one by one but I still get a segmentation fault</p>
  
  <p>The pointer was made here:</p>
  
  <pre><code>char* ptr;
  </code></pre>
  
  <p>I passed it through the function as this:</p>
  
  <pre><code>fillDict(ptr,&amp;size);
  int fillDict(char* ptr,int *size)
  </code></pre>
  
  <p>And it reads the file and puts it into the array here:</p>
  
  <pre><code>    int i = -1;
      int numb;
      int wsize;
      while (fgets(word,30,file)!=NULL)
      {
              if (i==-1)
              {
                      if(word[strlen(word)-1]=='\n')
                      {
                              word[strlen(word)-1] = 0;
                      }
                      numb = atoi(word);
                      ptr = malloc(sizeof(char));
              }
              else
              {
                      if(word[strlen(word)-1]=='\n')
                      {
                              word[strlen(word)-1] = 0;
                      }
                      wsize = wsize+strlen(word);
                      ptr = realloc(ptr,wsize);
  
                      int j = 0;                     //added from here
                      while(j&lt;strlen(word)-1)
                      {
                              printf("%d\n",j);
                              ptr[j] = word[j];      //crashes here
                              j++;
                      }
                      ptr[j] = '\0';                 //to here
  
                      size++;
              }
              i++;
      }
      printf("%s",ptr[0]);           //but fails here
      fclose(file);
  </code></pre>