What is wrong with this function?

mattg1981

Senior member
Jun 19, 2003
957
0
76
something in my typecasting I think is doing it ...

void solveRoots (int a, int b, int c, double * root1, double * root2) {
if ((double)(pow(b,2) - (4 * a * c)) < 0)
root1 = root2 = -1.0; // Line 33

else {
root1 = (double)((-b + sqrt((pow(b,2) - (4 * a * c)))) / 2 * a); // Line 36
root2 = (double)((-b - sqrt((pow(b,2) - (4 * a * c)))) / 2 * a); // Line 37
}
}

Here is the error:
mgimbl@linux:~/programming/activities/quadraticPtr> make
gcc -o quad quad.c
quad.c: In function `solveRoots':
quad.c:33: error: incompatible types in assignment
quad.c:36: error: incompatible types in assignment
quad.c:37: error: incompatible types in assignment
make: *** [quad] Error 1
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
What is the difference between double x and double* x ?

What is the difference between x = y and *x = y ?
 

mattg1981

Senior member
Jun 19, 2003
957
0
76
As far as parameters:
double x - pass in a 'copy' or just the data stored in the address of 'x'
double * x - creates a pointer to the memory address 'x' ... so you can actually change the data in it

x = y - ' x gets y ... or make the value of x = to the value of y'
*x = y - set what is stored in x equal to or the same thing as y
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Good, so what is the type of root1, root2? double or pointer to double?

Why isn't it valid c/c++ to say:
double x, y ;
y = 12.5 ;
x = &amp;y ;

?

Pointers are one of the more complicated parts of c/c++ so learning them well is a Good Thing.
 

mattg1981

Senior member
Jun 19, 2003
957
0
76
Ok .. I see now.

But I have another problem (and as you can see I was a java programmer before and you must have been laughing at my syntax). Anyways, here is some my new syntax

void solveRoots(int a, int b, int c, double * root1Ptr, double * root2Ptr) {
double underRad, exp;
exp = pow(b, 2);

if (4 * a * c - exp < 0)
*root1Ptr = *root2Ptr = -1.0;

else {
underRad = sqrt(exp - (4 * a * c));
* root1Ptr = (double)((-b + underRad) / (2 * a));
* root2Ptr = (double)((-b - underRad) / (2 * a));
}
}

But I am getting this error:
mgimbl@linux:~/programming/activities/quadraticPtr> make
gcc -o quad quad.c
/tmp/ccAIXkKH.o(.text+0xf9): In function `solveRoots':
: undefined reference to `pow'
/tmp/ccAIXkKH.o(.text+0x163): In function `solveRoots':
: undefined reference to `sqrt'
collect2: ld returned 1 exit status
make: *** [quad] Error 1

am I not calling pow() right and sqrt() right ? I have #include<math.h> at the top ...
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
hmm, in Windows VC++ the proper include is

#include <math.h>

so that's correct, though the code sample also includes

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

: puzzled; &amp;&amp; :(
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: mattg1981
Ok .. I see now.

But I have another problem (and as you can see I was a java programmer before and you must have been laughing at my syntax). Anyways, here is some my new syntax

void solveRoots(int a, int b, int c, double * root1Ptr, double * root2Ptr) {
double underRad, exp;
exp = pow(b, 2);

if (4 * a * c - exp < 0)
*root1Ptr = *root2Ptr = -1.0;

else {
underRad = sqrt(exp - (4 * a * c));
* root1Ptr = (double)((-b + underRad) / (2 * a));
* root2Ptr = (double)((-b - underRad) / (2 * a));
}
}

But I am getting this error:
mgimbl@linux:~/programming/activities/quadraticPtr> make
gcc -o quad quad.c
/tmp/ccAIXkKH.o(.text+0xf9): In function `solveRoots':
: undefined reference to `pow'
/tmp/ccAIXkKH.o(.text+0x163): In function `solveRoots':
: undefined reference to `sqrt'
collect2: ld returned 1 exit status
make: *** [quad] Error 1

am I not calling pow() right and sqrt() right ? I have #include<math.h> at the top ...

you need to link the math library by using -lm on gcc