1

I created a function to calculate the Euclidean distance between two points in C (written in the Codeblocks IDE), however some errors occurred:

error: expected ')' before 'p1'|

error: expected expression before ',' token|

The errors above occurred inside the function float Euclidean(struct point p1,struct point p2)

Below is my code:

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

struct point { float x, y, z; };

int main() {

    struct point p1 = { 2.1, 3.0, 1.2 };
    struct point p2 = { 0.5, 0.5, -0.2 };

    float Euclidean(struct point p1, struct point p2);

    return 1;
}

float Euclidean(struct point p1, struct point p2) {

    float distance;

    distance = (float)sqrt((float)powf((struct point p1[0] - struct point p2[0]), 2)+/n
                           (float)powf((struct point p1[1] - struct point p2[1]), 2)+/n
                           (float)powf((struct point p1[2] - struct point p2[2]), 2));

    return distance;

    printf("the distance between p1 and p2 is %.3f", distance);
};

I suspect there is some issue with my typecasting, but I can't figure out why (I'm relatively new to C). Could someone give me some hints?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Randy Chen
  • 107
  • 8
  • When using a struct, just use `p1.x` etc. No need to declare it again. And this struct is not an array. `p1[0]` is not correct. – Damien Jan 06 '22 at 17:29

2 Answers2

2

There are few mistakes in the provied code. You do not need to add struct point annotation to usages of variables. So wherever you need to use variable, you can directly reference them like Euclidean(p1, p2)

Another point is you need to declare/define function before using it.

For accessing value within a struct, you use dot notation, not index into it. So you need to use p1.x instead of p1[0].

Any statement after return is not run, so your print statement will not be run in the function.

Following is corrected code that compiles and run in GCC 9.3.0:

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

struct point {float x, y, z; };
float Euclidean(struct point p1,struct point p2);

int main()
{
    struct point p1 = {2.1,3.0,1.2};
    struct point p2 = {0.5,0.5,-0.2};

    float distance = Euclidean(p1, p2);
    return 0;
}

float Euclidean(struct point p1,struct point p2){
    float distance;
    distance = (float)sqrt((float)pow((p1.x-p2.x),2)+
                           (float)pow((p1.y-p2.y),2)+
                           (float)pow((p1.z-p2.z),2));
    printf("the distance between p1 and p2 is %.3f\n",distance);
    return distance;
};

As said in the other answer, it will be good if you understand the basics of C syntax from some book.

e_a
  • 61
  • 4
2

In addition to @e_a good answer ...

... rather than use double functions for a float problem, use float functions. Drop the casting.

//float distance;
//distance = (float)sqrt((float)pow((p1.x-p2.x),2)+
//                       (float)pow((p1.y-p2.y),2)+
//                       (float)pow((p1.z-p2.z),2));

float distance = sqrtf(powf((p1.x - p2.x),2) + 
                       powf((p1.y - p2.y),2) +
                       powf((p1.z - p2.z),2));
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • It is also probably better to use basic multiplication instead of `pow` for squaring the coordinate differences. – chqrlie Jan 06 '22 at 18:03
  • 1
    @chqrlie Agreed and not agree. Good compilers often analyze standard library functions and see code like `powf(x,2)` and then emit efficient code that does not use a true function call, but `x*x` as it is the same functionality - perhaps let the compiler's do the compiler's job. The above move from `double` to `float` functions imparts a slightly different functionality as `(float)pow((p1.x-p2.x),2)` does a [double rounding](https://en.wikipedia.org/wiki/Rounding#Double_rounding) versus `powf((p1.x-p2.x),2)`, so the compiler cannot substitute on its own. As with most FP - many subtleties. – chux - Reinstate Monica Jan 06 '22 at 18:23
  • thanks! My code is able to run, but somehow the result is not printed despite the ```printf``` statement inside the function(above ```return```)...what's going on here? – Randy Chen Jan 06 '22 at 19:57
  • @chux-ReinstateMonica I removed all the ```\n``` as well as the ```return``` statement in my code, but somehow no value is printed – Randy Chen Jan 06 '22 at 20:03
  • Why remove `"\n"`? Why remove `return`? – chux - Reinstate Monica Jan 06 '22 at 20:03
  • @RandyChen After `distance = .....`, use `printf("Distance %g\n", distance); return distance;`. ` – chux - Reinstate Monica Jan 06 '22 at 20:06
  • that works! thanks – Randy Chen Jan 06 '22 at 20:07
  • @RandyChen Either you are not using a C compiler (using a C++ one?) or using a compiler that is 23 years behind the C standard of C99. What compiler are you using? – chux - Reinstate Monica Jan 06 '22 at 20:11