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

In summary: But the error message itself is pretty clear about what is wrong.In summary, the conversation is discussing an error message in the code "error C2440: '=' : cannot convert from 'double' to 'double *'". The code is new to C and weak in pointers and the error is likely due to a formatting error in a printf line. The error message is clear and provides the necessary information to locate and fix the problem.
  • #1
tnecniv
16
0
what is wrong with my code that i keep getting this error "error C2440: '=' : cannot convert from 'double' to 'double *'"

i am new to c and very weak in pointer.

#include<stdio.h>

//Function Prototypes//
int read(double pointco[][2]);
void obtainStatistics(double pointco[][2],int n,double *a);

//======================start of codes===========================//
void main(){
double pointco[100][2];
int i,j,n,p;
double temp[4]={0};


n=read(pointco);

for(p=0;p<5;p++){
printf("%lf %lf %lf \n",obtainStatistics(pointco,n,&temp[p])); //%lf %lf
}

for (i=0;i<n;i++){
for(j=0;j<2;j++){
printf("%lf",pointco[j]);
}//for j
printf("\n");
}//for i

}//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[j] ) != EOF)
n++;
}//j
}//i
n=n/2;

fclose(fp);
return n;

}//close read
//======================Read point.txt========================//
void obtainStatistics(double pointco[][2],int n,double *a)
{
int i,j,k;
double temp1,temp2;

for(i=0;i<n;i++){
for(j=0;j<1;j++){

a[0]+=pointco[j];
}
}//find sum of all x
for(i=0;i<n;i++){
for(j=1;j<2;j++){

a[1]+=pointco[j];
}
}//find sum of all y

for(i=0;i<n;i++){
for(j=0;j<1;j++){
temp1=pointco[j];
for(k=1;k<2;k++){
temp2=pointco[k];
a[2]+=temp1*temp2;
}//k
}//j
}//i//find sum of x*y


return;
}
 
Technology news on Phys.org
  • #2


It would have helped if you indicated what line the error was at.

I notice the line
printf("%lf %lf %lf \n",obtainStatistics(pointco,n,&temp[p])); //%lf %lf​
makes no sense...



But anyways, I didn't get the error you mentioned when I tried compiling your code...

But surely the error message is clear? You had an assignment, the right-hand side was of type double, and the left-hand side was of type double*.
 
  • #3


I'm with Hurkyl on this. That printf line makes no sense. Your format control string indicates that printf should format three double numbers, but there is only one expression after the format string, not three. And besides that, obtainStatistics is a void function, so it doesn't return anything and you are attempting to evaluate it in the printf call.

Your error probably gives the file in which the error occurred and the line number. From that information you can find exactly where the problem is.
 

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

What does "Pointer error error C2440" mean?

This error refers to a specific type of error in C++ programming where there is an issue with assigning a value to a pointer.

Why does the error say "cannot convert from 'double' to 'double *'"?

This part of the error message indicates that there is a mismatch between the type of value being assigned and the type of the pointer variable. In this case, a double value is being assigned to a pointer to a double, which is not allowed.

How can I fix this error?

The error can be fixed by making sure that the types of the value being assigned and the pointer variable are compatible. If necessary, the value can be converted to the correct type using a typecast.

What are some common causes of this error?

One common cause of this error is when a variable of the wrong type is mistakenly passed to a function that expects a pointer as an argument. Another cause can be incorrect use of pointers or type mismatches in pointer assignments.

Can this error be prevented?

Yes, this error can be prevented by carefully checking the types of values being assigned to pointers and ensuring that they are compatible. Good programming practices, such as properly declaring and initializing pointers, can also help prevent this error from occurring.

Similar threads

Replies
4
Views
2K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
5
Views
1K
Replies
25
Views
2K
Replies
7
Views
2K
Replies
1
Views
2K
Replies
4
Views
1K
Back
Top