Can somebody please help me solve segmentation fault. c program

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

struct triangle
{
int a;
int b;
int c;
};

typedef struct triangle triangle;
void sort_by_area(triangle* tr, int n) {
/**
* Sort an array a of the length n
*/

int i,j=0,k;
int m=0,l=0,o=0;
int p[n];
int area[n]; 
for (i=0;i<n;i++)
{
    p[i]=((tr[i].a+tr[i].b+tr[i].c)/2);
    area[i]=sqrt(p[i]*(p[i]-tr[i].a)*(p[i]-tr[i].b)*(p[i]-tr[i].c));
}

for (k = 0; k < n; ++i) 
    {

        for (j = k + 1; k < n; ++j)
        {

            if (area[k] > area[j]) 
            {

                
                m=tr[k].a;
                tr[k].a=tr[j].a;
                tr[j].a=m;

                l=tr[k].b;
                tr[k].b=tr[j].b;
                tr[j].b=l;

                o=tr[k].c;
                tr[k].c=tr[j].c;
                tr[j].c=o;

            }

        }

    }

}

int main()
{
int n;
scanf("%d", &n);
triangle *tr = malloc(n * sizeof(triangle));
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &tr[i].a, &tr[i].b, &tr[i].c);
}
sort_by_area(tr, n);
for (int i = 0; i < n; i++) {
printf("%d %d %d\n", tr[i].a, tr[i].b, tr[i].c);
}
return 0;
}