title: Why does calling a function modify the value of an array of pointer to function that weren't given in parameter?
body: <p>I've got a struct that contains a pointer to function and an array of pointers to function. I'm passing the first pointer (not the array) as parameter of a function which is supposed to tell me whether an array of integers is sorted or not (it can be in ascending or descending order and this is defined by compFct, which is the pointer to function given in parameter).</p>
  
  <p>Unfortunately that function is changing the values in the array of pointers in my struct (without changing the value of my pointer to function given in parameter).</p>
  
  <p>Using gdb I managed to know when were made the changes in my array. It appears to be modified after the first printf in the printSorted function.  </p>
  
  
  
  <p>My typedef :</p>
  
  <pre class="lang-c prettyprint-override"><code>typedef int (*PtrCompFct)(int, int);
  typedef int (*PtrSortFct)(int*, int, int, PtrCompFct);
  </code></pre>
  
  <p>The struct :</p>
  
  <pre class="lang-c prettyprint-override"><code>typedef struct
  {
      int nbFct;
      PtrCompFct compFct;
      PtrSortFct *sortFct;
  } SortCompFct_s;
  </code></pre>
  
  <p>Here is how I'm calling my function (userChoices is of SortCompFct_s type):</p>
  
  <pre class="lang-c prettyprint-override"><code>printSorted(myArr, myArrSize, userChoices-&gt;compFct);
  </code></pre>
  
  <p>And the fonction that is changing my structure :</p>
  
  <pre class="lang-c prettyprint-override"><code>int printSorted(int *arr, int arrSize, PtrCompFct compFct)
  {
      for (int i=0; i&lt;(arrSize-1); i++)
      {
          if (compFct(arr[i+1], arr[i]))
          {
              //this is when my array of pointers to function is modified
              printf("The array isn't sorted\n\n");
              return 0;
          }
      }
      printf("The array is sorted\n\n");
      return 1;
  }
  </code></pre>
  
  <p>With gdb before the printf I have :</p>
  
  <pre class="lang-c prettyprint-override"><code>(gdb) print main::userChoices-&gt;sortFct[0]
  $36 = (PtrSortFct) 0x5555555548ea &lt;quickSort&gt;
  </code></pre>
  
  <p>and after :</p>
  
  <pre class="lang-c prettyprint-override"><code>(gdb) print main::userChoices-&gt;sortFct[0]
  $37 = (PtrSortFct) 0x7fffffffddc0
  </code></pre>
  
  <p>As you can see the pointer to my quickSort function has been modified. </p>
  
  <p>EDIT : include of the simplified and verifiable code, the thing is that this code is working properly, even with the printSorted function</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  
  typedef int (*PtrCompFct)(int, int);
  typedef int (*PtrSortFct)(int*, int, int, PtrCompFct);
  
  typedef struct
  {
      int nbFct;
      PtrCompFct compFct;
      PtrSortFct *sortFct;
  } SortCompFct_s;
  
  typedef SortCompFct_s *PtrSortCompFct_s;
  
  void initTab(int *arr, int arrSize)
  {
      time_t t;
      srand(time(&amp;t));
      for (int i=0; i&lt;arrSize; i++)
      {
          arr[i] = rand();
      }
  }
  
  int ascendingSort(int elmt1, int elmt2)
  {
      return (elmt1 &lt; elmt2);
  }
  
  int descendingSort(int elmt1, int elmt2)
  {
      return (elmt1 &gt; elmt2);
  }
  
  void switche(int *arr, int ind1, int ind2)
  {
      int temp = arr[ind1];
      arr[ind1] = arr[ind2];
      arr[ind2] = temp;
  }
  
  int bubbleSort(int *arr, int ind1, int ind2, PtrCompFct fctComp)
  {
      int sorted;
      for (int i=ind1; i&lt;ind2; i++)
      {
          sorted = 1;
  
          for (int j=0; j&lt;ind2; j++)
          {
              if (fctComp(arr[j+1], arr[j]))
              {
                  switche(arr, j, j+1);
                  sorted = 0;
              }
          }
  
          if (sorted) return 0;
      }
  
      return 0;
  }
  
  void printArr(int *arr, int arrSize)
  {
      for (int i=0; i&lt;arrSize; i++)
      {
          printf("%16d\n", arr[i]);
      }
  }
  
  int printSorted(int *arr, int arrSize, PtrCompFct compFct)
  {
      for (int i=0; i&lt;arrSize-1; i++)
      {
          if (compFct(arr[i+1], arr[i]))
          {
              //this is when my array of pointers to function is modified
              printf("The array isn't sorted\n\n");
              return 0;
          }
      }
      printf("The array is sorted\n\n");
      return 1;
  }
  
  PtrSortCompFct_s menu(void)
  {
      PtrSortCompFct_s userChoices;
      PtrSortFct arrSortFct[] = {bubbleSort};
  
      if ((userChoices = malloc(3*sizeof(int))) != NULL)
      {
          userChoices-&gt;nbFct = 1;
          userChoices-&gt;compFct = ascendingSort;
          userChoices-&gt;sortFct = arrSortFct;
      }
  
      return userChoices;
  }
  
  int main(void)
  {
      int arrSize = 10;
      int arr[arrSize];
      initTab(arr, arrSize);
      PtrSortCompFct_s userChoices;
  
      if ((userChoices = malloc(3*sizeof(int))) != NULL) userChoices = menu();
  
      printArr(arr, arrSize);
      printSorted(arr, arrSize, userChoices-&gt;compFct);
  
      userChoices-&gt;sortFct[0](arr, 0, arrSize-1, userChoices-&gt;compFct);
  
      printArr(arr, arrSize);
      printSorted(arr, arrSize, userChoices-&gt;compFct);
  
      return 0;
  }
  </code></pre>