CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Basic problem with Structures

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Arclite



Joined: 02 Jul 2004
Posts: 16
Location: UK

View user's profile Send private message Send e-mail

Basic problem with Structures
PostPosted: Mon Jul 19, 2004 2:33 pm     Reply with quote

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
PostPosted: Mon Jul 19, 2004 3:01 pm     Reply with quote

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

View user's profile Send private message

another apporach
PostPosted: Tue Jul 20, 2004 1:53 am     Reply with quote

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 Wink
Arclite



Joined: 02 Jul 2004
Posts: 16
Location: UK

View user's profile Send private message Send e-mail

Tried and trusted
PostPosted: Tue Jul 20, 2004 4:15 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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