|
|
View previous topic :: View next topic |
Author |
Message |
Arclite
Joined: 02 Jul 2004 Posts: 16 Location: UK
|
Basic problem with Structures |
Posted: Mon Jul 19, 2004 2:33 pm |
|
|
Hi guys,
I havn't used PCWH for long and i need to know how to pass a structure
as a parameter of a function. I'm told by the compiler that i can't pass structs as parameters, so what is the method? I have no problems with pointers if required
I have:
struct Point
{
int16 X;
int16 Y;
int16 Z;
} NewPnt, LastPnt;
//---------------
Interpolate(struct Point A, struct Point B)
{
int16 Gradient;
// do stuff with A.X, ,A.Y, A.Z, and B.X, B.Y, B.Z
Gradient = A.X -B.X;
blah, blah, blah...
}
//---------------
void Main()
{
Interpolate(NewPnt,LastPnt);
} |
|
|
Ttelmah Guest
|
Re: Basic problem with Structures |
Posted: Mon Jul 19, 2004 3:01 pm |
|
|
Arclite wrote: | Hi guys,
I havn't used PCWH for long and i need to know how to pass a structure
as a parameter of a function. I'm told by the compiler that i can't pass structs as parameters, so what is the method? I have no problems with pointers if required
I have:
struct Point
{
int16 X;
int16 Y;
int16 Z;
} NewPnt, LastPnt;
//---------------
Interpolate(struct Point A, struct Point B)
{
int16 Gradient;
// do stuff with A.X, ,A.Y, A.Z, and B.X, B.Y, B.Z
Gradient = A.X -B.X;
blah, blah, blah...
}
//---------------
void Main()
{
Interpolate(NewPnt,LastPnt);
} |
As you say, use pointers.
Code the definition as:
Interpolate(struct Point *A, struct Point *B)
code the call as:
Interpolate(&NewPnt,&LastPnt);
Inside the routine, code the actions on the structure, using the '->' construct, instead of the '.'. So:
Gradient = A->X -B->X;
If you want to return a structure, code the defintion as:
struct Point * Interpolate(struct Point *A, struct Point *B)
Inside the routine, declare a _static_ structure to contain the result, and put the values into this, say:
static struct Point result;
Then write to the elements of this as needed, and at the end of the routine, return the address of this with:
return(&result);
In the main routine, have a variable to hold this address, defined as:
struct Point * result_val;
And again talk to the elements with:
result_val->X etc.
Best Wishes |
|
|
kypec
Joined: 20 Sep 2003 Posts: 54
|
another apporach |
Posted: Tue Jul 20, 2004 1:53 am |
|
|
I have coded it in my program like this:
Code: |
void interpolate(struct point &a,struct point &b) {
int16 gradient;
gradient=a.x-b.x;
}
//function is called later like this
interpolate(a,b);
|
for me it is more convenient and readable, try it if you like |
|
|
Arclite
Joined: 02 Jul 2004 Posts: 16 Location: UK
|
Tried and trusted |
Posted: Tue Jul 20, 2004 4:15 am |
|
|
Thanks for the responses,
Ttelmah:
I've tried your approach, and it works. No surprise given that i had not read the manual on '->'. I've made much headway on a single chip 3 axis controller for that.
And again, thanks.
kypec:
I understand your method, the only problem being that i need to isolate the RAM within the function when i use/modify it. I therefore use scratch memory (i think that is what it's called) to for local variables help troubleshoot the code. Secondly, the code will be used for a larger number of axes in the future (6-10), so it helps now to make thing water tight.
Thanks to you too. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|