Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

gotenks05

macrumors member
Original poster
Jan 1, 2009
78
0
Well, after doing Java programming for about a year, as a hobby. I decided to try and get a grasp on C. Right now, I am trying to make a program with a custom function that finds the distance between two points on a 3D graph and got it written correctly, as far as I know, but GCC gives me an error.

the error is:

‘y1’ redeclared as different kind of symbol


However, I look at the code and there is nothing that changes the datatype of that variable and it is only declared once. What am I doing wrong?

the source code looks like this:

Code:
#include <stdio.h>
#include <math.h>

float x1, x2, y1; 
float y2, z1, z2;
float dist3d (float ux, float uy, float uz, float vx, float vy, float vz);

int main()
{
	printf("Enter the first point in form of (x, y, z) without commas or parenthesis:  ");
	scanf ("%f %f %f", &x1, &y1, &z1);

	printf("Enter the second point in form of (x, y, z) without commas or parenthesis:  ");
	scanf ("%f %f %f", &x2, &y2, &z2);

	printf("The distance between (%f, %f, %f) and (%f, %f, %f) is:  %f\n", x1, y1, z1, x2, y2, z2, dist3d(x1, y1, z1, x2, y2, z2));

return 0;
} /* end of program */

float dist3d(float ux, float uy, float uz, float vx, float vy, float vz)
{
	return sqrt(pow((vx - ux), 2) + pow((vy - uy), 2) + pow((vz - uz), 2));
}
 
You're getting that error because 'y1' is also a function in the math library. It is for bessel functions (along with y0, and several others).

I would make x1/2, y1/2, z1/2 local variables inside main and not use global variables. That would eliminate your error. Besides, as your program stands right now, they do not need to be global anyway.
 
Edit: Beat by 2 minutes. Damn.

Believe it or not, y1() is the name of a library function. (It computes the Bessel function of the second kind and order 1.)

Thus, you can't declare another global variable called y1.

You should move the lines

Code:
float x1, x2, y1; 
float y2, z1, z2;

into main().
 
Here's where the flat C namespace can bite you.

Try 'man y1' on the command line, and you'll see it's already defined in math.h to be a bessel function (along with j0, j1, jn, y0, and yn). That means, you aren't able to declare a variable with the same name in the global scope, just like you wouldn't be able to name a global variable 'main'.

But really, there's no reason for those variables to be in the global scope in the first place. Just move them into the main function, and you'll be fine.

EDIT: Not fast enough... I guess 3rd time's a charm, yes?
 
float x1, x2, y1;
float y2, z1, z2;
float dist3d (float ux, float uy, float uz, float vx, float vy, float vz);

As a rule, you should only use "float" instead of "double" if you have a very good reason to do do. If you don't know which one to prefer, use "double". It will save you trouble.
 
As a rule, you should only use "float" instead of "double" if you have a very good reason to do do. If you don't know which one to prefer, use "double". It will save you trouble.

True. Usually floats are only used if you're doing work with graphics or really need to save space. As an approximate rule of thumb, floats are accurate to about 6 decimal places, and doubles are accurate to about 15. Keep that in mind.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.