Pointer error need help error C2440: '=' : cannot convert from 'double' to 'double *'

This is a discussion on Pointer error need help error C2440: '=' : cannot convert from 'double' to 'double *' within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include<math.h> //Function Prototypes// int read(double pointco[][2]); void obtainStatistics(double pointco[][2],int n,double a[]); void trendLine(double temp[],int p, double m[]); double ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    2

    Pointer error need help error C2440: '=' : cannot convert from 'double' to 'double *'

    Code:
    #include<stdio.h>
    #include<math.h>
    
    //Function Prototypes//
    int read(double pointco[][2]);
    void obtainStatistics(double pointco[][2],int n,double a[]);
    void trendLine(double temp[],int p, double m[]);
    double closestPoint(double pointco[][2], int, double, double);
    //======================start of codes===========================//
    void main(){
    double pointco[100][2];
    int i,j,n;
    double temp[4];
    double m[2];
    double *p;
    
    n=read(pointco);
    obtainStatistics(pointco,n,temp);
    trendLine(temp,n,m);
    
    printf("------------------------------\n");
    printf("Q1b.obtainStatistics");
    printf("The sum of x^2: %lf \n",temp[0]);
    printf("The sum of y^2: %lf \n",temp[1]);
    printf("The sum of xy: %lf \n",temp[2]);
    printf("The mean of x: %lf \n",temp[3]);
    printf("The mean of y: %lf \n",temp[4]);
    printf("------------------------------\n");
    
    printf("Q1c.trendLine");
    printf("m= %lf \n",m[0]);
    printf("c= %lf \n",m[1]);
    printf("Equation of regression line: y= %lfx+%lf \n",m[0],m[1]);
    printf("------------------------------\n");
    //=============================================================//
    printf("Q2a Closest point");
    p=closestPoint(pointco,n,m[0],m[1]);
    printf("Closest point = (%lf, %lf)\n",*p,*(p+1));
    
    //============================================================//
    printf("Closest point = (%lf, %lf)\n",*p,*(p+1));
    printf("------------------------------\n");
    printf("Q1a.Statistics");
    for (i=0;i<n;i++){
    		for(j=0;j<2;j++){
    			printf("%lf",pointco[i][j]);	
    		}//for j
    		printf("\n");
    	}//for i
    printf("------------------------------\n");
    
    }//main close
    
    //======================End of main=============================//
    int read(double pointco[][2]) {
    int i,j,n=0;
    
    	FILE *fp = fopen("points.txt", "r");
    
    	for(i=0;i<100;i++){
    		for(j=0;j<2;j++){
    			if(fscanf(fp,"%lf", &pointco[i][j] ) != EOF)
    				n++ ;
    		}//j
    	}//i
    n=n/2;
    
    fclose(fp);
    return n;
    
    }//close read
    //======================Read point.txt========================//
    void obtainStatistics(double pointco[][2],int z,double a[])
    {
    int i,j,k;
    double temp1,temp2;
    for(i=0;i<5;i++){
    	a[i]=0;
    }
    for(i=0;i<z;i++){
    	for(j=0;j<1;j++){
    
    		a[0]+=pointco[i][j]*pointco[i][j];
    		a[3]+=pointco[i][j];
    	}
    }
    a[3]=a[3]/z;
    //find sum of all x^2
    
    for(i=0;i<z;i++){
    	for(j=1;j<2;j++){
    
    		a[1]+=pointco[i][j]*pointco[i][j];
    		a[4]+=pointco[i][j];
    	}
    }
    a[4]=a[4]/z;
    //find sum of all y^2
    
    for(i=0;i<z;i++){
    	for(j=0;j<1;j++){
    		temp1=pointco[i][j];
    		for(k=1;k<2;k++){
    			temp2=pointco[i][k];
    			a[2]+=temp1*temp2;
    		}//k
    	}//j
    }//i//find sum of x*y
    
    
    
    	return;
    }
    
    void trendLine(double temp[],int p,double m[]){
    	m[0]=0;
    	m[1]=0;
    	
    m[0]= (temp[2]-(p*(temp[3]*temp[4])))/(temp[0]-p*(temp[3]*temp[3]));//m
    m[1]= temp[4]-temp[3]*m[0];//c
    return;
    }
    
    double closestPoint(double pointco[][2], int q, double m, double c){
    int i,j;
    double distance;
    double temp;
    for(i=0;i<q;i++){
    	for(j=0;j<1;j++){
    	temp=pointco[i][j];
    	}
    	for(j=1;j<2;j++){
    		distance=(pointco[i][j]+m*temp+c)/(sqrt(pointco[i][j]*pointco[i][j]+temp*temp));
    	} 
    }
    return distance;
    }
    Why am i encountering the above error.. i am very weak in pointer. please advise

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Look at the line that the error occurred - 'p' is a double*, but the function returns a double. See the conflict?

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    You need to indent the code.
    Also, main returns int, not void.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    2
    Quote Originally Posted by Sebastiani View Post
    Look at the line that the error occurred - 'p' is a double*, but the function returns a double. See the conflict?
    what can i change to make this code run?
    how do i use it to pass by reference the values from my closest point function to my main

    please advise.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Okay, say you have two structure types - A and B, and then this:

    Code:
    
    A foo();
    // ...later...
    B b = foo();
    1) Why doesn't that work?
    2) What do you think needs to happen to get it to work?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. pointer with double asterisk
    By skringla in forum C Programming
    Replies: 10
    Last Post: 11-27-2008, 06:33 AM
  3. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21