why am i getting segmentation fault when giving input?

#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,r,c,i,j;
scanf("%d",&n);
while(n–)
{
scanf("%d",&r);
c=r;
int *a = malloc(sizeof(int)*r);
for(i=0;i<r;i++)
{
a[i] = malloc(sizeof(int)*c);
}
for(i=0;i<r;i++)
{
for(j=0;j<=i;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=r-1;i>0;i++)
{
for(j=0;j<i;j++)
{
if(a[i][j]>a[i][j+1])
{ a[i-1][j] = a[i-1][j]+a[i][j];}
else
{ a[i-1][j] = a[i-1][j] + a[i][j+1];}
}
}
printf("%d\n",a[0][0]);
}
}

Please format the code properly. There could be problem with your Malloch statement.

I think you need to remove the & operator before a in scanf since a is now a pointer to pointer. There’s a minute difference between array and pointers.

I did whatever u are saying but it’s still not working,again showing the same error.
Please help me out.

when i debug code error shows in If statement below
if(a[i][j]>a[i][j+1])

Hey buddy,

I’m not sure if this works or not but one possible reason is this line

for(i=r-1;i>0;i++) // from your code

for(i=r-1;i>=0;i–) // change it to this

for loop should decrease instead of i++

hope this helps!

can you provide the question, you are trying to solve so get the better idea of your solution.

Thanks a lot!!!