Could you please help me to fix the code. I guess that the problem is in the pointer but I have no idea how to fix it.
Below is my code. The problem is in **Function 2**(on the bottom of the code). Before I added it my code worked perfect. But when I added this **Function 2** and I got **zsh: segmentation fault**. As I understand from reading similar problems the reason is in the pointers declared in **Function 2**. Or maybe the code in **Function 2** is not correct. Please see 2 screenshots: 1) if I run the program as it listed below; 2) if I remove **Function 2** from `int main(void)`

and run code without it.
with Function 2
if function 2 is excluded

```
#include <stdio.h>
#include <math.h>
double F(double x);
double Lagrange(double new_x, double *x, double *y, int p); //Function 1
double error(double new_x, double *x, double *y, int n); //Function 2
int main(void)
{
int a=2, b=3, n=20, n1, n2;
double h=0.2, x[100], y[100], x1, y1, max, R;
printf(" x y\n");
n1=(b-a)/h+1;
for (int i=0; i<n1; i++)
{
x[i]=a+i*h;
y[i]=F(x[i]);
printf(" %.2f %.7f\n",x[i],y[i]);
}
max=0;
printf(" x1 y1 Error\n");
n2=(b-a)/(h/2)+1;
for (int j=0; j<n2; j++)
{
x1=a+j*h/2;
y1=Lagrange(x1, x, y, n);
R=error(x1, x, y, n);
if (R>max) max=R;
printf (" %.2f %.7f %e\n", x1, y1, R);
}
printf("\nMax error on [%d, %d] = %e\n\n", a, b, max);
return 0;
}
double F(double x)
{
return exp(-pow(x-3,2))+exp(-pow(x-5,2));
}
//Function 1
double Lagrange(double new_x, double *x, double *y, int n)
{
double p, lagrange_p=0;
int i, j, n0=0;
while (new_x>x[n0+n]) n0++;
for (i=n0; i<=n0+n; i++)
{
p=1;
for (j=n0; j<=n+n0; j++)
{
if (j!=i)
{
p*=(new_x-x[j])/(x[i]-x[j]);
}
}
lagrange_p+=p*y[i];
}
return lagrange_p;
}
//Function 2
double error(double new_x, double *x, double *y, int n)
{
double h=0.2, p, s=1, delta[n][n];
int c, f=1, i, j, k;
for (k=0; k<=n; k++)
{
delta[k][0]=y[k];
}
for (i=1; i<=n; i++)
{
for (j=0; j<=n-1; j++)
delta[i][j]=delta[i-1][j+1]-delta[i-1][j];
}
for (c=1; c<=n+1; c++)
{
f=f*c;
}
for (i=0; i<=n; i++)
{
s*=new_x-x[i];
}
return s*delta[k][0]/(f*pow(h,n+1));
}
```

Read more here: https://stackoverflow.com/questions/67018397/segmentation-fault-something-wrong-with-pointers

### Content Attribution

This content was originally published by Gelios at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.