title: create 2d array dynamically and copy txt file. Segmentation error
body: <p>I have keep getting segmentation error for this code. Any suggestions how to fix it? I know I am suppose to use free() after every malloc(), but I don't see how in my function since I am returning the created array. It;s weird this code works from home but not in school where I have to make it work. Any suggestions will be highly appreciated.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;ctype.h&gt;
  void changeToLower(const char *regular, char *lowerCaseOnly)
  {
      size_t ctr;
      size_t len = strlen(regular);
      int i=0;
      while(i&lt;len) 
      {
          lowerCaseOnly[i] = tolower((unsigned char) regular[i]); 
          i++;    
      }
      lowerCaseOnly[len] = '\0';
  }
  char ** enterFile(int *size, char * nameofFile )
  {
      int l;
      int ne;
      int messageSize         =165;
      char fname[40]          ;//="dictionary2.txt";
      FILE *file              =NULL;
      int i                   ;
      char **dArray           =NULL;
      int localsize           =0;
      if(strcmp(fname, "quit\n") == 0){
          exit(-1);
      }
      printf("Enter the name of the dictionary file you'd to use: ");
      fgets(fname,100, stdin);
      strtok(fname, "\n"); 
      strcpy(nameofFile,fname);
      file = fopen(fname,"r");
      if(file==NULL){ printf("Error could not find file;\n");}//In case file DNE
      fscanf(file, "%d", size);       
      localsize=(*(size)*2)+1;
      *size=localsize;
      //Allocation of memory for 2D array
      dArray =  malloc(localsize*sizeof( char*)); 
      for(i=0;i&lt;localsize;i++){
          dArray[i]=malloc(messageSize*sizeof(char));
      }
      i=0;
      while(!feof(file))
      {
          fgets(dArray[i],messageSize, file);
          strtok(dArray[i], "\n");
          i++;
      }   
      fclose(file);
      printf("Dictionary File Loaded\n");
      return dArray;    
  }
  int main()
  {
      int size              =0;
      int ne, counter         ;
      char **dArray      =NULL;                   //pointer to array  DOUBLE ARRAY
      char option             ;
      int x                 =1;                                       //Neede DND
      char nameofDictionary[40]=" ";
      while(x!=5)
      {
          printf("|------------------------------------------------------|\n");
          printf("| Enter Option 1: To open dictionary file              |\n");
          scanf(" %c", &amp; option);
          while((ne=getchar()) != '\n' &amp;&amp; ne!= EOF );//====&gt; DELETE
          //printf("You entered this option:\t %c\n", option);
          switch(option)
          {
          case '1':
              dArray=enterFile(&amp;size, nameofDictionary);     
              //  printf("main file name now is: %s\n", nameofDictionary);
              break;
          case '2':
              //option2(dArray, &amp;size);
              break;
          case '3':   
              //option3(dArray, &amp;size);
              break;
          case '4':
              //dArray=option4(dArray,&amp;size, nameofDictionary);
              //  printf("this should say futbol %s",dArray[151]);
              break;
          case '5':
              x=5;
              printf("Exit program.");
              break;
          default:
              printf("&gt;&gt;&gt;&gt;INVALID: Enter a valid input, try again\n");
          }
      }
      return 0;
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/mhw9G.png" alt="enter image description here"></p>
  
  <p>TEXTFILE only contains these sentences:</p>
  
  <pre><code>75
  nevermind
  nvm
  not much
  nm
  no problem
  np
  people
  ppl
  talk to you later
  ttyl
  because
  cuz
  i don't know
  idk
  as soon as possible
  asap
  yeah
  ya
  how are you
  hru
  you
  u
  are
  r
  late
  l8
  at
  @
  good morning
  gud morn
  see you later
  c u l8r
  hello
  lo
  love
  &lt;3
  as far as i know
  afaik
  away from keyboard
  afk
  thanks
  thx
  today
  2day
  before
  b4
  great
  gr8
  at the moment
  atm
  by the way
  btw
  oh my gosh
  omg
  rolling on the floor laughing
  rofl
  if i remember correctly
  iirc
  message
  msg
  please
  plz
  for your information
  fyi
  in my opinion
  imo
  just kidding
  jk
  you only live once
  yolo
  today
  2day
  tomorrow
  2moro
  tonight
  2nite
  alright
  aight
  also known as
  aka
  ask me anything
  ama
  with
  w/
  without
  w/o
  be right back
  brb
  on my way
  omw
  busy
  bz
  can
  cn
  cool
  coo
  disconnected
  d/c
  definitely
  def
  them
  dem
  too long; didn't read
  tl;dr
  estimated time of arrival
  eta
  favorite
  fav
  facebook
  fb
  got to go
  g2g
  good game
  gg
  good job
  gj
  congratulations
  gratz
  what
  wat
  i don't care
  idc
  i know, right
  ikr
  whatever
  meh
  minutes
  mins
  real life
  rl
  skater
  sk8r
  seriously
  srsly
  sorry
  sry
  whats up
  sup
  to be determined
  tbd
  throwback thursday
  tbt
  too much information
  tmi
  text
  txt
  thank you
  ty
  what the freak
  wtf
  </code></pre>