-1

I am trying to compute the largest of the sums of numbers that appear on the paths of a triangle of numbers starting from the top towards the base so that on each path the next number is located on the row below, more precisely either directly below or below and one place to the right. Getting a Segmentation fault. Don't know why??

#include<stdio.h>
#include<stdlib.h>

int func(int **arr, int n, int i,int max)
{
    int j,m,a,b,c;
    if(i==(n-1))
        return max;
    else
    {
        for(j=0;j<=i;j++)
        {
            a=*(*(arr+i)+j);
            b=*(*(arr+(i+1))+j);
            c=*(*(arr+(i+1))+(j+1));
            if(((a+b)>=(a+c))&&((a+b)>max))
            {
                max=(a+b);
                m=j;
            }
            if(((a+c)>(a+b))&&((a+c)>max))
            {
                max=(a+c);
                m=j+1;
            }
        }
        *(*(arr+(i+1))+m)=max;
        func(arr,n,i+1,max);
    }
}


int main()
{
    int n,array[100][100],i,j;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        for(j=0;j<=i;j++)
            scanf("%d",&array[i][j]);
    }
    printf("%d\n",func(array,n,0,0));
    return 0;
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
ncoder
  • 151
  • 1
  • 8
  • 1
    Running your program in [a debugger](http://www.gnu.org/software/ddd/) would be a good start to finding out where and why the segfault occurs. – Michael Jul 30 '14 at 17:34
  • 1
    While `a=*(*(arr+i)+j);` is acceptable syntax, most people would expect that to be written as `a = arr[i][j]` – Bill Lynch Jul 30 '14 at 17:35
  • 2
    …or just listen to your compiler “42: warning: passing argument 1 of ‘func’ from incompatible pointer type” – mafso Jul 30 '14 at 17:35

1 Answers1

1

Because you are passing an int array[100][100] to an int ** and they are not interchangeable:

int func(int **arr, int n, int i,int max)

Must be:

int func(int (*arr)[100], int n, int i,int max)

Take a look to this related question.

Community
  • 1
  • 1
David Ranieri
  • 39,972
  • 7
  • 52
  • 94