473,788 Members | 2,855 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

struct point not identified by gcc

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;p 1.z=5;
p2.x=5;p1.y=2;p 1.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);
}

Feb 22 '07 #1
39 3113
On Feb 22, 9:27 am, "DanielJohn son" <diffuse...@gma il.comwrote:
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]

Feb 22 '07 #2
DanielJohnson wrote:
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;p 1.z=5;
p2.x=5;p1.y=2;p 1.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.

Feb 22 '07 #3
On Feb 22, 12:43 pm, "santosh" <santosh....@gm ail.comwrote:
DanielJohnson wrote:
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;p 1.z=5;
p2.x=5;p1.y=2;p 1.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.
I did all your changes and now I get this error

undefined reference to `sqrt'

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

Feb 22 '07 #4
On Thu, 22 Feb 2007 09:27:06 -0800, DanielJohnson wrote:
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
Feb 22 '07 #5
DanielJohnson wrote:
On Feb 22, 12:43 pm, "santosh" <santosh....@gm ail.comwrote:
DanielJohnson wrote:
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.
<snip code and corrections>
Correct these errors and try to compile again.

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++.

Feb 22 '07 #6

santosh wrote:
DanielJohnson wrote:
On Feb 22, 12:43 pm, "santosh" <santosh....@gm ail.comwrote:
DanielJohnson wrote:
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.
<snip code and corrections>
Is math.sqrt() correct or just sqrt()

The latter, with an argument. The former is C++.
Correction: It's not even C++.

Feb 22 '07 #7
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;p 1.z=5;
p2.x=5;p2.y=2;p 2.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);
}

Feb 22 '07 #8
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++.
Thanks it worked. Do I always need to compile using -lm switch. I was
completely unawre if this.
Feb 22 '07 #9
On Feb 22, 10:20 am, "DanielJohn son" <diffuse...@gma il.comwrote:
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++.

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.

Feb 22 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
9082
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for the purpose of learning to work with C++. It therefore makes some sense for me to give the situation the amount of consideration presented below. To be quite honest, I'm amazed at the amount there is to say about such a seemingly simple...
3
647
by: Robert Rota | last post by:
Need help figuring out why my program core dumps and if I have the structures right. If you are interested in helping me I can send you a copy of the code. The program is supposed to mimic a 3 level pagetable. Thank you. struct _PageTable { struct _PageTable **PageArray; unsigned int *FrameArray;
9
7206
by: werasm | last post by:
Hi all, What is the difference between: typedef struct { ... } MyS1; ....and...
6
359
by: Eric | last post by:
This IS material from a CS class on object oriented programming. It is NOT my homework. Consider the following: struct A {short i; void f () {cout << "A::f()\n";}}; struct B : A {long j; void f () {cout << "B::f()\n";} void g () {cout << "B::g()\n";}}; { A* const a = new B; // dangerous (1)
21
10586
by: Zytan | last post by:
Is it possible, as in C? I don't think it is. Just checking. Zytan
9
1627
by: Carramba | last post by:
Hi! I want to have a struct representing a table, with int's and float. But I seem to get only stuckdump.. and not really sure why , hope you can help! Thanxk you in advance #include <stdio.h> #include <stdlib.h>
27
5519
by: arkmancn | last post by:
Any comments? thanks. Jim
6
368
by: BIll Cunningham | last post by:
pg 128 of kandr2 speaks of a struct struct point{ int x; int y; }; I understand this but what is it saying about using intialize variables like such.
4
1848
by: Lew Pitcher | last post by:
(having trouble getting my reply through - hopefully, third time is a charm) On November 11, 2008 19:53, in comp.lang.c, BIll Cunningham (nospam@nspam.invalid) wrote: Why an int? Because K&R chose to use an int. Why not a double? Because K&R chose to use an int. Note the phrase "syntactically analogous". It means that the /syntax/ of
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10113
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5402
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.