How to implement a running variable in a recursive function
<p>At the moment I am coding a program in which I need a variable <code>count</code> that increments every time I call the function. In my case I have a recursive function and want to know how many iterations the program does.</p>

<p>I simplified the code by computing the factorial of a number.</p>

<p>My first approach does not work and ends up with warning messages:</p>

<pre><code>#include &lt;stdio.h&gt;

int factorial(unsigned int i, int *count)
{
   *count += 1;
   if(i &lt;= 1)
   {
     return 1;
   }
   return i * factorial(i - 1, &amp;count);
}  

int  main()
{
   int i = 10;
   int count = 0;
   printf("%d Iterations, Factorial of %d is %d\n", count, i, factorial(i, &amp;count));
   return 0;
}


warning: passing argument 2 of ‘factorial’ from incompatible pointer type
</code></pre>

<p>My second approach does not work either but also does not ends up with any warning messages.</p>

<pre><code>#include &lt;stdio.h&gt;

int factorial(unsigned int i, int count)
{
   count += 1;
   if(i &lt;= 1)
   {
     return 1;
   }
   return i * factorial(i - 1, count);
}   

int  main()
{
   int i = 10;
   int count = 0;
   printf("%d Iterations, Factorial of %d is %d\n", count, i, factorial(i, count));
   return 0;
}
</code></pre>

<p>How can I make it run? Any ideas? I use Ubuntu and gcc.</p>