struct point not identified by gcc

D

DanielJohnson

I wrote a simply program with a 3 D coordinate called point. I am
adding two points, taking their distance etc. gcc compiler is not
identifying the struct. Here is the code. I get bunch of errors. The
main ones are : useless storage class specifier in empty declaration,
'point' undeclared (first use in this function), 'p1' undeclared
(first use in this function) etc. Any help will be appreciated. Thanks

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

/* create a 3d point struct*/
typedef struct point{
int x,y,z;
};

/* Function declaration*/
float dist(point *, point *);
point add(point *, point *);
void display(point *);

int main(void)
{
point p1,p2,p3;
p1.x=2;p1.y=4;p1.z=5;
p2.x=5;p1.y=2;p1.z=6;
printf("\n dist = %f", dist(&p1,&p2));
add(&p1, &p2);
display(&p3);
return;
}

float dist(point *a, point *b)
{
float length=0.0;
length = sqrt( (a->x-b->x)*(a->x-b->x)+ (a->y-b->y)*(a->y-b->y)+
(a->z-b->z)*(a->z-b->z));
return length;
}

point add(point *a, point *b)
{
point temp;
temp.x=a->x + b->x;
temp.y=a->y + b->y;
temp.z=a->z + b->z;
return temp;
}

void display(point *a)
{
printf("\npoint= %d,%d,%d\n",a->x,a->y,a->z);
}
 
U

user923005

I wrote a simply program with a 3 D coordinate called point. I am
adding two points, taking their distance etc. gcc compiler is not
identifying the struct. Here is the code. I get bunch of errors. The
main ones are : useless storage class specifier in empty declaration,
'point' undeclared (first use in this function), 'p1' undeclared
(first use in this function) etc. Any help will be appreciated. Thanks

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

/* create a 3d point struct*/
typedef struct point{
int x,y,z;

};

/* Function declaration*/
float dist(point *, point *);
point add(point *, point *);
void display(point *);

Try:
typedef struct tag_point {
int x,y,z;
} point;
From the C FAQ:
Section 2. Structures, Unions, and Enumerations

2.1: What's the difference between these two declarations?

struct x1 { ... };
typedef struct { ... } x2;

A: The first form declares a "structure tag"; the second declares a
"typedef". The main difference is that you subsequently refer
to the first type as "struct x1" and the second simply as "x2".
That is, the second declaration is of a slightly more abstract
type -- its users don't necessarily know that it is a structure,
and the keyword struct is not used when declaring instances of it.

2.2: Why doesn't

struct x { ... };
x thestruct;

work?

A: C is not C++. Typedef names are not automatically generated for
structure tags. See also question 2.1 above.

[snip]
 
S

santosh

DanielJohnson said:
I wrote a simply program with a 3 D coordinate called point. I am
adding two points, taking their distance etc. gcc compiler is not
identifying the struct. Here is the code. I get bunch of errors. The
main ones are : useless storage class specifier in empty declaration,
'point' undeclared (first use in this function), 'p1' undeclared
(first use in this function) etc. Any help will be appreciated. Thanks

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

/* create a 3d point struct*/
typedef struct point{
int x,y,z;
};

Change this to:

typedef struct {
int x;
int y;
int z;
} point;
/* Function declaration*/
float dist(point *, point *);
point add(point *, point *);
void display(point *);

int main(void)
{
point p1,p2,p3;
p1.x=2;p1.y=4;p1.z=5;
p2.x=5;p1.y=2;p1.z=6;

Why not use more whitespace to aid in clarity? You also seem to be
overwriting the values in p1.y and p1.z.
printf("\n dist = %f", dist(&p1,&p2));

Add a newline after %f as well. Also as per the initialisations given
above, p2.y, p2.z, p3.x, p3.y, p3.z are all uninitialised. If you
operate on uninitialised objects you'll most likely get back useless
results.
add(&p1, &p2);

The function add returns a local object, (as given below). You're not
assigning the return value to anything here, thus loosing it.
display(&p3);
return;

Return an explicit value; don't return random values.
}

float dist(point *a, point *b)
{
float length=0.0;
length = sqrt( (a->x-b->x)*(a->x-b->x)+ (a->y-b->y)*(a->y-b->y)+
(a->z-b->z)*(a->z-b->z));

You do realise that b->y and b->z are uninitialised don't you? You
might also want to split up the messy function call into more readable
steps.
return length;
}

point add(point *a, point *b)
{
point temp;
temp.x=a->x + b->x;
temp.y=a->y + b->y;
temp.z=a->z + b->z;
return temp;
}

void display(point *a)
{
printf("\npoint= %d,%d,%d\n",a->x,a->y,a->z);
}

Correct these errors and try to compile again.
 
U

u plz

I wrote a simply program with a 3 D coordinate called point. I am
adding two points, taking their distance etc. gcc compiler is not
identifying the struct. Here is the code. I get bunch of errors. The
main ones are : useless storage class specifier in empty declaration,
'point' undeclared (first use in this function), 'p1' undeclared
(first use in this function) etc. Any help will be appreciated. Thanks

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

/* create a 3d point struct*/
typedef struct point{
int x,y,z;
};

You need a name for the typedef, so you could do something like:

typedef struct {
int x, y, z;
} point;

-Alok
 
D

DanielJohnson

Change this to:

typedef struct {
int x;
int y;
int z;
} point;



Why not use more whitespace to aid in clarity? You also seem to be
overwriting the values in p1.y and p1.z.


Add a newline after %f as well. Also as per the initialisations given
above, p2.y, p2.z, p3.x, p3.y, p3.z are all uninitialised. If you
operate on uninitialised objects you'll most likely get back useless
results.


The function add returns a local object, (as given below). You're not
assigning the return value to anything here, thus loosing it.


Return an explicit value; don't return random values.



You do realise that b->y and b->z are uninitialised don't you? You
might also want to split up the messy function call into more readable
steps.






Correct these errors and try to compile again.

I did all your changes and now I get this error

undefined reference to `sqrt'

Is math.sqrt() correct or just sqrt()
 
S

santosh

DanielJohnson said:
I did all your changes and now I get this error

undefined reference to `sqrt'

Under most UNIX systems, the math library is seperate and not linked
in by default. Add the '-lm' command line switch, (without the
quotes), to cc when you compile it.
Is math.sqrt() correct or just sqrt()

The latter, with an argument. The former is C++.
 
D

DanielJohnson

Here is the updated program but it doesnt compile and says undefined
refrence to sqrt. Can you please tell whats going wrong in this
variant of the program.


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

/* create a 3d point struct*/
typedef struct{
int x;
int y;
int z;
} point;

/* Function declaration*/
double dist(point *, point *);
point add(point *, point *);
void display(point *);

int main(void)
{
point p1,p2,p3;
p1.x=2;p1.y=4;p1.z=5;
p2.x=5;p2.y=2;p2.z=6;
printf("\n dist = %lf \n", dist(&p1,&p2));
p3 = add(&p1, &p2);
display(&p3);
return 0;
}

double dist(point *a, point *b)
{
double length=0.0;
length = sqrt( (a->x-b->x)*(a->x-b->x) + (a->y-b->y)*(a->y-b->y)+
(a->z-b->z)*(a->z-b->z) );
return length;
}

point add(point *a, point *b)
{
point temp;
temp.x=a->x + b->x;
temp.y=a->y + b->y;
temp.z=a->z + b->z;
return temp;
}

void display(point *a)
{
printf("\npoint= %d,%d,%d\n",a->x,a->y,a->z);
}
 
D

DanielJohnson

Under most UNIX systems, the math library is seperate and not linked
in by default. Add the '-lm' command line switch, (without the
quotes), to cc when you compile it.


The latter, with an argument. The former is C++.

Thanks it worked. Do I always need to compile using -lm switch. I was
completely unawre if this.
 
U

user923005

Thanks it worked. Do I always need to compile using -lm switch. I was
completely unawre if this.

How did you miss this when you read the C-FAQ:

14.3: I'm trying to do some simple trig, and I am #including <math.h>,
but I keep getting "undefined: sin" compilation errors.

A: Make sure you're actually linking with the math library. For
instance, under Unix, you usually need to use the -lm option, at
the *end* of the command line, when compiling/linking. See also
questions 13.25, 13.26, and 14.2.
 
S

santosh

DanielJohnson said:
Thanks it worked. Do I always need to compile using -lm switch. I was
completely unawre if this.

No, only when you use one or more functions from the math library. The
interface for the math functions are in the headers math.h, float.h,
complex.h, tgmath.h and fenv.h. The last three are specific to C99.

BTW, this is an instance of the more general requirement that if you
use functions from library XY, then you should usually supply an -lXY
switch to the compiler. The exception is the C standard library
itself, which is linked in automatically. The standard doesn't specify
the details of compilation and linkage, so for more details consult
your system's compiler and linker manuals.
 
F

Flash Gordon

DanielJohnson wrote, On 22/02/07 17:55:
I did all your changes and now I get this error

undefined reference to `sqrt'

Is math.sqrt() correct or just sqrt()

1) Why on earth would you want math.sqrt()?
2) You need to learn how to drive the tools you are using and specific
tools are not on topic here.
3) You need to read the comp.lang.c FAQ at http://c-faq.com/
specifically Q14.3, but you should always consult it before asking a
question in case it is covered.
 
F

Flash Gordon

DanielJohnson wrote, On 22/02/07 18:15:
Here is the updated program but it doesnt compile and says undefined
refrence to sqrt. Can you please tell whats going wrong in this

<snip>

Did you wait a reasonable time to see if anyone would ask last time you
asked this question (without the source which in this case was not
needed) before asking again? No. I fetched news, wrote a reply, and when
I did the next post/fetch cycle I got you asking again as well as two
other people answering the question and me posting my answer.

In future, please wait at least a day before assuming no one has
answered, and even then check the FAQ in case the reason no one bothers
to answer is because it is covered there.
 
D

DanielJohnson

In future, please wait at least a day before assuming no one has
answered, and even then check the FAQ in case the reason no one bothers
to answer is because it is covered there.

Santosh suggested me to do something and I tried his changes and still
it won't work. The thing which he told me was to use the switch -lm
for compiling correctly.

OK Sir, Learning the rules of the game.
 
K

Keith Thompson

DanielJohnson said:
typedef struct point{
int x,y,z;
};
[...]

As several others have pointed out, this is your problem. You create
a structure type "struct point" and, in the same declaration, attempt
to create a typedef (alias) for it -- but you don't provide a name for
the typedef. This isn't necessarily illegal, but you should have
gotten a warning; if you didn't, crank up the warning level on your
compiler.

If you want a typedef, the way to do it is:

typedef struct {
int x, y, z;
} point;

(you can optionally have a tag name after the word "struct"). Or
you can lose the typedef altogether and just do this:

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

This creates a type called "struct point". Since it already has a
perfectly good name, there's no need to create an alias for it using
"typedef". If you use this declaration, you'll need to refer to the
type as "struct point" rather than just "point":

struct point p0;
struct point p1;

In some people's opinions, including mine, this is a cleaner approach;
it's more verbose, but that's a good thing, since it's obvious from
the declaration that p0 and p1 are struct objects.

On the other hand, a lot of programmers prefer to use typedefs so they
can have a one-word name for the type. Either approach is legitimate;
even if you choose one or the other, you should understand both.

See section 2 of the comp.lang.c FAQ, <http://www.c-faq.com/>.
 
J

jacob.navia

DanielJohnson a écrit :
Thanks it worked. Do I always need to compile using -lm switch. I was
completely unawre if this.
You need the -lm switch when using the gcc software. This switch is
never used in other compilers not under unix, that are smart enough to
including the C math functions by default.

We have often discussed here this msfeature of gcc but they will never
change it as it seems.
 
R

Roberto Waltman

DanielJohnson said:
Santosh suggested me to do something and I tried his changes and still
it won't work. The thing which he told me was to use the switch -lm
for compiling correctly.

"for linking correctly"
OK Sir, Learning the rules of the game.

One rule down, another 999,999,999 to go. ;)


Roberto Waltman

[ Please reply to the group,
return address is invalid ]
 
M

Mark McIntyre

DanielJohnson a écrit :

We have often discussed here this msfeature of gcc but they will never
change it as it seems.

Its not a gcc-ism, please don't mischaracterise it. You're probably
aware that all DOS compilers exhibited this same feature for many
years. I think some early Windows ones even did it (certainly C6 and
Watcom's C compiler did, even when compiling windows-compatible code).
The reason, as far as I recall, is their roots in hardware that didn't
provide floating point support.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
K

Keith Thompson

jacob.navia said:
DanielJohnson a écrit :
You need the -lm switch when using the gcc software. This switch is
never used in other compilers not under unix, that are smart enough to
including the C math functions by default.

We have often discussed here this msfeature of gcc but they will never
change it as it seems.

gcc is not the only compiler that behaves this way. Sun's "cc" under
Solaris, for example, also requires "-lm" to link the math library.
It may be specific to Unix-like systems; it's not specific to gcc.

I agree that the explicit "-lm" shouldn't be necessary, but I don't
think it's a huge deal.
 
R

Richard Tobin

jacob.navia said:
You need the -lm switch when using the gcc software. This switch is
never used in other compilers not under unix, that are smart enough to
including the C math functions by default.

This doesn't have anything much to do with gcc. Before gcc was
thought of, unix needed -lm to link the maths library, and today the
Mac I'm using doesn't need -lm even though its compiler is gcc.

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top