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

  • Thread starter Thread starter tnecniv
  • Start date Start date
  • Tags Tags
    Convert Error
AI Thread Summary
The discussion centers on a coding error in C, specifically "error C2440: '=' : cannot convert from 'double' to 'double *'." The issue arises from a misunderstanding of function return types and printf usage. The function `obtainStatistics` is defined as a void function, meaning it does not return a value, yet it is being used in a printf statement that expects three double values. This mismatch leads to the compilation error. Additionally, the format string in printf indicates three doubles are expected, but only one expression is provided, compounding the issue. The error message should indicate the specific line number, which can help pinpoint the problem. Overall, the key takeaway is that the code incorrectly attempts to use a void function in a context that requires a return value.
tnecniv
Messages
15
Reaction score
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


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*.
 


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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

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