grid unique paths - recursion
<p>I am trying to solve the grid unique paths problem. The problem involves finding the number of possible unique paths in a 2D grid starting from top left (0,0) to the bottom right (say A,B). One can only move right or down. Here is my initial attempt:</p>

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

int count=0;
void uniquePathsRecur(int r, int c, int A, int B){
    if(r==A-1 &amp; c==B-1){
        count++;
        return;
    }
    if(r&lt;A-1){
        return uniquePathsRecur(r++,c,A,B);
    }
    if(c&lt;B-1){
        return uniquePathsRecur(r,c++,A,B);
    }

}

int uniquePaths(int A, int B) {

    if(B==1 | A==1){
        return 1;
    }

    uniquePathsRecur(0,0,A,B);
    return count;
}


int main(){

printf("%d", uniquePaths(5,3));

return 0;
}
</code></pre>

<p>I end up getting segmentation fault: 11 with my code. I tried to debug in gdb and i get the following:</p>

<pre><code>lldb) target create "a.out"
Current executable set to 'a.out' (x86_64).
(lldb) r
Process 12171 launched: '&lt;path to process&gt;/a.out' (x86_64)
Process 12171 stopped
* thread #1: tid = 0x531b2e, 0x0000000100000e38 a.out`uniquePathsRecur + 8, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x7fff5f3ffffc)
    frame #0: 0x0000000100000e38 a.out`uniquePathsRecur + 8
a.out`uniquePathsRecur:
-&gt;  0x100000e38 &lt;+8&gt;:  movl   %edi, -0x4(%rbp)
    0x100000e3b &lt;+11&gt;: movl   %esi, -0x8(%rbp)
    0x100000e3e &lt;+14&gt;: movl   %edx, -0xc(%rbp)
    0x100000e41 &lt;+17&gt;: movl   %ecx, -0x10(%rbp)
(lldb)
</code></pre>

<p>What is wrong with the above code?</p>