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

  • Thread starter Thread starter tnecniv
  • Start date Start date
  • Tags Tags
    Convert Error
Click For Summary
SUMMARY

The forum discussion addresses a common C programming error: "error C2440: '=' : cannot convert from 'double' to 'double *'." The issue arises from the misuse of the printf function, where the return value of the void function obtainStatistics is incorrectly treated as a double. The code attempts to print three double values using a single void function call, leading to the type mismatch error. To resolve this, the printf statement should be corrected to reflect the actual return values intended for output.

PREREQUISITES
  • Understanding of C programming syntax and semantics
  • Knowledge of pointers and their usage in C
  • Familiarity with function prototypes and return types in C
  • Basic file I/O operations in C
NEXT STEPS
  • Review C programming concepts related to pointers and memory management
  • Learn about function return types and how to handle void functions in C
  • Practice debugging techniques for identifying and resolving compilation errors
  • Explore the use of printf for formatted output in C, including format specifiers
USEFUL FOR

Beginner C programmers, students learning about pointers and functions, and developers seeking to improve their debugging skills in C programming.

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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K