access error!

hey...this is my code. when i compile, there are no errors but when i run my program, there is this access error that comes up and doesn't let me run my program. my code is in C but compiler used is visual C++.

the error is:
Unhandled exception at 0x0087182e in preeti_newlab9.exe: 0xC0000005: Access violation reading location 0x00000000.

my code is:

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

double ObtainStatistics(double y[100][1],int z);
double trendLine(double *a,double *b,double *c,double *d,double *e,int x);
double * closestPoint(double y[100][1],int x,double g,double c);

int main()
{
int i=0,a;
double points[100][1];
double s1, s2, s3, m1, m2,m,c,*p;
double *sumX2=&s1, *sumY2=&s2, *sumXY=&s3, *meanX=&m1, *meanY=&m2,*grad=&m, *intercept=&c;


FILE* fin=fopen("points.txt","r");

while(1)
{
fscanf(fin, "%lf %lf", &points[i][0],&points[i][1]);
if(feof(fin))
break;
i++;
}

fclose(fin);

//for(a=0;a<i;a++)
//printf("%lf %lf\n", points[a][0], points[a][1]);

printf("Value of n is: %d\n", i);

ObtainStatistics(points,i);

trendLine(sumX2, sumY2, sumXY, meanX, meanY,i);

printf("y= %lfx + %lf",*grad, *intercept);

p=closestPoint(points,i,m,c);
printf("Closest point = (%lf, %lf)\n",*p,*(p+1));

return 0;
}

double ObtainStatistics(double y[100][1],int z)
{
double *sumX2=NULL, *sumY2=NULL, *sumXY=NULL, *meanX=NULL, *meanY=NULL,*grad=NULL, *intercept=NULL;
int b;
double sum1=0,sum2=0,sum3=0;

for(b=0;b<z;b++)
{
sum1 += ((y[b][0])*(y[b][0]));
*sumX2 = sum1;
sum2 +=((y[b][1])*(y[b][1]));
*sumY2 = sum2;
sum3 +=(y[b][0])*(y[b][1]);
*sumXY = sum3;
*meanX = *sumX2/z;
*meanY = *sumY2/z;
}

return 0;
}

double trendLine(double *a,double *b,double *c,double *d,double *e,int x)
{
double *grad=NULL, *intercept=NULL;
double h,p,q,r;

h=*d * *e;
p=*d * *d;
q=*c - x * h;
r=*a - x * p;
*grad=q/r;
*intercept=*e - *d * *grad;

return 0;

}

double * closestPoint(double y[100][1],int x,double g,double c)
{

double *grad=NULL, *intercept=NULL, *p=NULL;
double sort[100], dist[100];
int b;
double temp,n,d;
int k, j, m, hold;

for(b=0; b<x; b++)
{
n = abs(*grad * y[x][0] + y[x][1] + *intercept);
d = sqrt(*grad * *grad + 1);
temp = n/d;
sort[b] = temp;
}

for(b=0;b<100;b++)
{
dist[b] = sort[b];
}

for (k = 0; k <x-1; k++)
{
/* Exchange min with next array value */
m = k;
for (j = k; j < x; j++)
{
if (sort[j] < sort[m])
m = j;
if (m != k)
{
hold = sort[k];
sort[k] = sort[m];
sort[m] = hold;
}
}
}

for(b=0;b<x;b++)
{
dist[b] == sort[0];
break;
}

double *ret = &y[b][0];

return ret;
}


thanks in advance!
You are dereferencing a NULL pointer:

1
2
3
double *sumX2=NULL, *sumY2=NULL, *sumXY=NULL, *meanX=NULL, *meanY=NULL,*grad=NULL, *intercept=NULL;

*sumX2 = sum1; //<-- sumX2 is still NULL 
Last edited on
oh so am i not supposed to initialize it to NULL? but if i don't, i get an error saying undeclared identifier.
that's why i initialized it NULL...
how can i fix this?
You seem confused on how pointers work...I would suggest reading this:

http://www.cplusplus.com/doc/tutorial/pointers/
thanks so much for the link...i browsed through and it seems very helpful.
Topic archived. No new replies allowed.