title: Passing an integer to a function in C - Returning 0
body: <p>I am writing a program to mergesort an array of a struct that I have.</p>
  
  <p>I am trying to pass in the integer values for L and R, and when I printf the values in the mergesort function the values become 0.</p>
  
  <p>Here is my code in its entirely. The problem occurs on the Sorter.c call of mergesort. </p>
  
  <p>SORTER.C</p>
  
  <pre><code>#include "mergesort.h"
  #include "Sorter.h"
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  int main(int argc, char * argv[]){
  
      /*
      // checks if num of arguments is correct
      if (argc != 2){
          printf("Sorry, the input isn't in a correct format 1");
          return 1;
      }
  
      // checks if second argument is -c
      if (strcmp(argv[1],"-c") != 0){
          printf("Sorry, the input isn't in a correct format 2");
          return 1;
      }*/
  
      // stores third argument as the selected column to sort
      char selectedColumn[strlen(argv[2])];
      strcpy(selectedColumn,argv[2]);
  
      int numOfLines = 5043;
      //Generate this dynamically
  
      FILE *fp;
      fp = fopen("movie_metadata.csv","r");
  
      char buffer[1000];
      fgets(buffer, 1000, fp);
  
      //printf("The first character is: %s", line);
  
      Row *allRows = (Row*) malloc(sizeof(Row) * numOfLines);
  
      char * splitLine;
      char* lineCopy;
  
      int i;
      for (i = 0; i &lt; numOfLines; i++){
          fgets(buffer, numOfLines, fp);
          lineCopy = buffer;
  
          splitLine = strtok (lineCopy,",");
          color = splitLine;
  
          splitLine = strtok (NULL, ",");
          director_name = splitLine;
  
          splitLine = strtok (NULL, ",");
          num_critic_for_reviews = splitLine;
  
          splitLine = strtok (NULL, ",");
          duration = splitLine;
  
          splitLine = strtok (NULL, ",");
          director_facebook_likes = splitLine;
  
          splitLine = strtok (NULL, ",");
          actor_3_facebook_likes = splitLine;
  
          splitLine = strtok (NULL, ",");
          actor_2_name = splitLine;
  
          splitLine = strtok (NULL, ",");
          actor_1_facebook_likes = splitLine;
  
          splitLine = strtok (NULL, ",");
          gross = splitLine;
  
          splitLine = strtok (NULL, ",");
          genres = splitLine;
  
          splitLine = strtok (NULL, ",");
          actor_1_name = splitLine;
  
          splitLine = strtok (NULL, ",");
          movie_title = splitLine;
  
          splitLine = strtok (NULL, ",");
          num_voted_users = splitLine;
  
          splitLine = strtok (NULL, ",");
          cast_total_facebook_likes = splitLine;
  
          splitLine = strtok (NULL, ",");
          actor_3_name = splitLine;
  
          splitLine = strtok (NULL, ",");
          facenumber_in_poster = splitLine;
  
          splitLine = strtok (NULL, ",");
          plot_keywords = splitLine;
  
          splitLine = strtok (NULL, ",");
          movie_imdb_link = splitLine;
  
          splitLine = strtok (NULL, ",");
          num_user_for_reviews = splitLine;
  
          splitLine = strtok (NULL, ",");
          language = splitLine;
  
          splitLine = strtok (NULL, ",");
          country = splitLine;
  
          splitLine = strtok (NULL, ",");
          content_rating = splitLine;
  
          splitLine = strtok (NULL, ",");
          budget = splitLine;
  
          splitLine = strtok (NULL, ",");
          title_year = splitLine;
  
          splitLine = strtok (NULL, ",");
          actor_2_facebook_likes = splitLine;
  
          splitLine = strtok (NULL, ",");
          imdb_score = splitLine;
  
          splitLine = strtok (NULL, ",");
          aspect_ratio = splitLine;
  
          splitLine = strtok (NULL, ",");
          movie_facebook_likes = splitLine;
  
          splitLine = strtok (NULL, ",");
  
          Row row = { .color = color, .director_name = director_name, .num_critic_for_reviews = num_critic_for_reviews, .duration = duration, .director_facebook_likes = director_facebook_likes, .actor_3_facebook_likes = actor_3_facebook_likes,
              .actor_2_name = actor_2_name, .actor_1_facebook_likes = actor_1_facebook_likes, .gross = gross, .genres = genres, .actor_1_name = actor_1_name, .movie_title = movie_title, .num_voted_users = num_voted_users,
              .cast_total_facebook_likes = cast_total_facebook_likes, .actor_3_name = actor_3_name, .facenumber_in_poster = facenumber_in_poster, .plot_keywords = plot_keywords, .movie_imdb_link = movie_imdb_link,
              .num_user_for_reviews = num_user_for_reviews, .language = language, .country = country, .content_rating = content_rating, .budget = budget, .title_year = title_year, .actor_2_facebook_likes = actor_2_facebook_likes,
              .imdb_score = imdb_score, .aspect_ratio = aspect_ratio, .movie_facebook_likes = movie_facebook_likes
          };
  
          allRows[i] = row;
  
          //printf("%d - Director: %s and %d\n",i, *allRows[i].director_name, i);
      }
      printf("Director: %s\n", *allRows[0].director_name);
      /*int test = 5042;
      int* testptr = &amp;test;
      printf("%d", *testptr);
  
      int test2 = 5042;
      int* test2ptr = &amp;test2;
      printf("%d", *test2ptr);*/
  
      int test = 0;
      int test2 = 5042;
      printf("this: %d", test2);
  
      mergeSort(*allRows, test, test2, "director_name");
  
      printf("Director: %s\n", *allRows[0].director_name);
  
  
  
      return 0;
  }
  </code></pre>
  
  <p>SORTER.H</p>
  
  <pre><code>/*
   * Sorter.h
   *
   *  Created on: Sep 27, 2017
   *      Author: Peter and Umar
   */
  
  #ifndef SORTER_H_
  #define SORTER_H_
  
  
  
  typedef struct Row {
  
      char* color[100];
      char* director_name[100];
      int* num_critic_for_reviews;
      int* duration;
      int* director_facebook_likes;
      int* actor_3_facebook_likes;
      char* actor_2_name[100];
      int* actor_1_facebook_likes;
      int* gross;
      char* genres[500];
      char* actor_1_name[100];
      char* movie_title[100];
      int* num_voted_users;
      int* cast_total_facebook_likes;
      char* actor_3_name[100];
      int* facenumber_in_poster;
      char* plot_keywords[1500];
      char* movie_imdb_link[1000];
      int* num_user_for_reviews;
      char* language[40];
      char* country[40];
      char* content_rating[100];
      int* budget;
      int* title_year;
      int* actor_2_facebook_likes;
      int* imdb_score;
      int* aspect_ratio;
      int* movie_facebook_likes;
  
  
  
  } Row;
  
  char* color;
  char* director_name;
  char* actor_2_name;
  char* genres;
  char* actor_1_name;
  char* movie_title;
  char* actor_3_name;
  char* plot_keywords;
  char* movie_imdb_link;
  char* language;
  char* country;
  char* content_rating;
  
  int* num_critic_for_reviews;
  int* duration;
  int* director_facebook_likes;
  int* actor_3_facebook_likes;
  int* actor_1_facebook_likes;
  int* gross;
  int* num_voted_users;
  int* cast_total_facebook_likes;
  int* facenumber_in_poster;
  int* num_user_for_reviews;
  int* budget;
  int* title_year;
  int* actor_2_facebook_likes;
  int* imdb_score;
  int* aspect_ratio;
  int* movie_facebook_likes;
  
  #endif
  </code></pre>
  
  <p>MERGESORT.C</p>
  
  <pre><code>#include "mergesort.h"
  #include "Sorter.h"
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  
  
  // Merges two subarrays of arr[]
  // First subarray is arr[l..m]
  // Second subarray is arr[m+1..r]
  void merge(Row* arr[], int l, int m, int r, char column[]){
      int i, j, k;
      int n1 = m - l + 1;
      int n2 =  r - m;
  
      /* create temp arrays */
      Row* L[n1];
      Row* R[n2];
  
      /* Copy data to temp arrays L[] and R[] */
      for (i = 0; i &lt; n1; i++)
          *L[i] = *arr[l + i];
      for (j = 0; j &lt; n2; j++)
          *R[j] = *arr[m + 1+ j];
  
      /* Merge the temp arrays back into arr[l..r]*/
      i = 0; // Initial index of first subarray
      j = 0; // Initial index of second subarray
      k = l; // Initial index of merged subarray
      printf("right before 1\n");
      while (i &lt; n1 &amp;&amp; j &lt; n2)
      {
  
          //strcmp(column,"director_name") ==0
          //strcmp(column,"actor_2_name") ==0
          //strcmp(column,"genres") ==0
          //strcmp(column,"actor_1_name") ==0
          //strcmp(column,"movie_title") ==0
          //strcmp(column,"actor_3_name") ==0
          //strcmp(column,"plot_keywords") ==0
          //strcmp(column,"movie_imdb_link") ==0
          //strcmp(column,"language") ==0
          //strcmp(column,"country") ==0
          //strcmp(column,"content_rating") ==0
  
          // do this for all the different column names ^^^
          printf("right before\n");
          if (strcmp(column,"director_name") ==0)
          {
              printf("equals director name\n");
              if (strcmp(L[i]-&gt;director_name,R[j]-&gt;director_name) &lt;= 0){
                  printf("next level\n");
                  *arr[k] = *L[i];
                  i++;
              } else {
                  *arr[k] = *R[j];
                  j++;
              }
              k++;
          }
  
  //        if (L[i] &lt;= R[j])
  //        {
  //            arr[k] = L[i];
  //            i++;
  //        }
  //        else
  //        {
  //            arr[k] = R[j];
  //            j++;
  //        }
  //        k++;
      }
  
      /* Copy the remaining elements of L[], if there
         are any */
      while (i &lt; n1)
      {
          *arr[k] = *L[i];
          i++;
          k++;
      }
  
      /* Copy the remaining elements of R[], if there
         are any */
      while (j &lt; n2)
      {
          *arr[k] = *R[j];
          j++;
          k++;
      }
  }
  
  /* l is for left index and r is right index of the
     sub-array of arr to be sorted */
  void mergeSort(Row* arr[], int l, int r, char column[]){
      printf("%d\n", r);
  printf("called\n");
  printf("%d\n", l);
  printf("%d\n", r);
  
  //THIS IS PRINTING 0 FOR SOME REASON ^ need to debug
  
  //printf("%s\n", *arr[0]-&gt;director_name);
  //printf("%s\n", column);
  
  
  
  
      if (l &lt; r){
          printf("l &lt; r\n");
          // Same as (l+r)/2, but avoids overflow for
          // large l and h
          int m = l+(r-l)/2;
  
          // Sort first and second halves
          mergeSort(arr, l, m, column);
          mergeSort(arr, m+1, r, column);
  
          merge(arr, l, m, r, column);
      }
  
      printf("Merged!\n");
  }
  </code></pre>
  
  <p>The values that I pass in become zero. </p>
  
  <p>I have tried creating pointers and passing those instead, but the value is still zero! I am blowing my brains out over here....such a simple task that I am struggling with. I would appreciate any help, I've tried various online solutions but clearly I am missing something here..</p>
  
  <p>Thank you</p>