title: How to pass 2D char array to another function by reference
body: <p>I want to pass my 2D char array to another function by reference. I did same thing with my int ** it works perfectly. But when it come to char ** same method is broken. I try too many things. I read almost every question but I couldn't see my case.</p>
  <p>First of all I have a one char ** which is set to NULL and I pass it to another function by reference.</p>
  <p>This my main function</p>
  <pre><code>int main(){
      int **recommendU2 = NULL;
      char **bookNames = NULL;
      
      readFromCsv(&amp;recommendU2,&amp;bookNames,&amp;bookCount,&amp;userCount);
  
      printf(&quot;\n user[2][3] = %d \n&quot;,recommendU2[2][3]); //this works 
      printf(&quot;\n bookNames[1] = %s \n,bookNames[1]); //this doesn't works
      
      return 0;
  }
  </code></pre>
  <p>Here is my another function which reads from csv and process this data. It is do it purpose without and issue. It reads from csv and read data line by line and process it without a problem. But I can't access my char ** data. But I can reach my int ** data. I didn't notice any difference between too.</p>
  <pre><code>void readFromCsv(int ***user,char ***bookNames,int *bookCount,int *userCount){
      FILE * fpointer;
      int lineCount = 0;  
      int count;
      int sizes[3];  
      fpointer = fopen(&quot;recdataset.csv&quot;,&quot;r&quot;);
      char recommendationSheet[RECOMMENDATIONSIZE];
  
      while(fgets(recommendationSheet,RECOMMENDATIONSIZE,fpointer)){
          if(lineCount&gt;1){
              //this part is not important for this question
          }
          else if(lineCount == 0){
              userArraysMemoryAllocate(recommendationSheet,user,nuser,bookNames,&amp;sizes[0]);
              (*user)[2][3] = 4;
              //I can access this user data both within this function and main function
          }
          else if(lineCount == 1){
              //actually I send variables to another function which named getBookNames() but when I can't reach data I suspect it and move it's code to here. But this doesn't make any different too.
  
              char *p = recommendationSheet;
              int j = 0;
              int i = 0;
              while(1) {
                  char *p2 = strchr(p, ';');
                  if(p2 != NULL)
                      *p2 = '\0';
                  if(i&gt;0)
                      strcpy((*bookNames)[i],p);
                  printf(&quot;\n Book Name is = %s \n&quot;,(*bookNames)[i]); //This line works read bookNames correctly
                  i++;
                  if(p2 == NULL)
                      break;
                  p = p2 + 1;
              }
              printf(&quot;\nbookName = %s \n&quot;,(*bookNames)[2]); //this line doesn't work
          } 
          lineCount++;
      }
      fclose(fpointer);
  }
  </code></pre>
  <p>I couldn't make it work. I tried so many variations but none of them works. If you have any solution or any other idea to make this work I will appreciate it. Thanks in advance.</p>
  <p>Note : This program is too long to put here. So I only copied problematic part here. This includes so many functions calls. So If you don't understand anywhere please say it and I could add this part of code too.</p>
  <p><strong>Update</strong></p>
  <p>I add memory allocate function to readfromCsv function too. Here is the code. My problem still occurs. I thought I fixed it but it cause unnormal behaviour.</p>
  <pre><code>void readFromCsv(int ***user,char ***bookNames,int *bookCount,int *userCount,){
  FILE * fpointer;
  int lineCount = 0;  
  int i = 0;
  int j = 0;
  int len;
  int count;
  int sizes[3];
  fpointer = fopen(&quot;recdataset.csv&quot;,&quot;r&quot;);
  char recommendationSheet[RECOMMENDATIONSIZE];
  
  while(fgets(recommendationSheet,RECOMMENDATIONSIZE,fpointer)){
      len = strlen(recommendationSheet);
      if( recommendationSheet[len-1] == '\n' )
          recommendationSheet[len-1] = 0;
      if(lineCount&gt;1){
          //not important
      }
      else if(lineCount == 0){
          char *p = recommendationSheet;
          i = 0;
          j = 0;
          int row = 0;
          int col = 0;
          
          while(i&lt;3) {
              char *p2 = strchr(p, ';');
              if(p2 != NULL)
                  *p2 = '\0';
              if(i==0){
                  row = atoi(p);  
                  col = row;          
                  if((*bookNames = (char**)malloc(row * sizeof(char*))) == NULL){
                  printf(&quot;\nMemory allocation problem \n&quot;);
                  exit(0);
                  }
                  for(j = 0; j &lt; row; ++j){
                      if(((*bookNames)[j] = (char*)malloc(50 * sizeof(char))) == NULL){
                          printf(&quot;\nMemory allocation problem \n&quot;);
                          exit(0);
                      }
                  }
              }
              else if(i==1){
                  row = atoi(p);
                  if((*user = (int**)malloc(row * sizeof(int*))) == NULL){
                      printf(&quot;\nMemory allocation problem \n&quot;);
                      exit(0);
                  }           
                  for(j = 0; j &lt; row; ++j){
                      if(((*user)[j] = (int*)malloc(col * sizeof(int))) == NULL){
                          printf(&quot;\nMemory allocation problem \n&quot;);
                          exit(0);
                      }
                  }           
              }
              else if(i==2){
                  row = atoi(p);
                  if((*nuser = (int**)malloc(row * sizeof(int*))) == NULL){
                      printf(&quot;\nMemory allocation problem \n&quot;);
                      exit(0);
                  }           
                  for(j = 0; j &lt; row; ++j){
                      if(((*nuser)[j] = (int*)malloc(col * sizeof(int))) == NULL){
                          printf(&quot;\nMemory allocation problem \n&quot;);
                          exit(0);
                      }
                  }
              }
              sizes[i] = row;
              i++;
              if(p2 == NULL)
                  break;
              p = p2 + 1;
          }
          (*user)[2][3] = 4; // this works still here and main function
      }
      else if(lineCount == 1){
          char *pMemory = recommendationSheet;
          j = 0;
          i = 0;
          while(1) {
              char *p2 = strchr(pMemory, ';');
              if(p2 != NULL)
                  *p2 = '\0';
              if(i&gt;0)
                  (*bookNames)[i] = pMemory;
              printf(&quot;\n %d. Book name is = %s \n&quot;,i,(*bookNames)[i]); // works perfectly
              i++;
              if(p2 == NULL)
                  break;
              pMemory = p2 + 1;
          }
      } 
      lineCount++;
  }
  
  fclose(fpointer);
  }
  </code></pre>
  <p>Still I couldn't access my char ** from my main function. I add memory allocation process. I though I fixed it but problem occurs again without any change. I didn't understand the problem at all.</p>