![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
rwjzownts
Joined: 19 Jul 2008 Posts: 16
|
passing a struct through one function into another |
Posted: Tue Sep 30, 2008 5:28 pm |
|
|
I would like to pass a struct into one function that will then pass the same struct into another function. Is this possible? I haven't been able to find anything about doing this. Here's my falied attempt:
In main:
Code: | if(servo0.counter > servo0.trigger)
{
//ledBlink(&servo0);
whichServo(&servo0);
}
if(servo1.counter > servo1.trigger)
{
//ledBlink(&servo1);
whichServo(&servo1);
}
|
First function:
Code: | void whichServo(servoStruct *structIn)
{
if(structIn->led == 0)
{
ledBlink(&structIn);
}
}
|
Second function:
Code: | void ledBlink(servoStruct *structIn)
{
if(structIn->led == 3)
{
output_toggle (PIN_C3);
}
else if(structIn->led == 4)
{
output_toggle (PIN_C4);
}
structIn->counter = 0;
} |
Here's all the code:
Code: | #include <18F2620.h>
#FUSES HS
#FUSES NOWDT
#FUSES NOPROTECT
#use delay(clock=20,000,000)
#use rs232 (baud = 9600, parity = N, xmit = PIN_C6, rcv = PIN_C7, bits = 8, ERRORS)
#define RAND_MAX 1000
#include <STDLIB.H>
typedef struct{
int16 counter;
int16 trigger;
int8 led;
}servoStruct;
servoStruct servo0;
servoStruct servo1;
void ledBlink(servoStruct *structIn);
void whichServo(servoStruct *structIn);
#INT_RTCC
void servocounter()
{
servo0.counter++;
servo1.counter++;
set_timer0(60535);
}
void main()
{
setup_comparator (NC_NC_NC_NC);
setup_timer_0 (RTCC_DIV_1|RTCC_INTERNAL);
enable_interrupts (INT_RTCC);
enable_interrupts (GLOBAL);
servo0.counter = 0;
servo0.trigger = 1000;
servo1.counter = 0;
servo1.trigger = 500;
servo0.led = 3;
servo1.led = 4;
delay_ms (500);
output_high(PIN_C3);
output_high(PIN_C4);
WHILE (true)
{
if(servo0.counter > servo0.trigger)
{
//ledBlink(&servo0);
whichServo(&servo0);
}
if(servo1.counter > servo1.trigger)
{
//ledBlink(&servo1);
whichServo(&servo1);
}
}
}
void whichServo(servoStruct *structIn)
{
if(structIn->led == 0)
{
ledBlink(&structIn);
}
}
void ledBlink(servoStruct *structIn)
{
if(structIn->led == 3)
{
output_toggle (PIN_C3);
}
else if(structIn->led == 4)
{
output_toggle (PIN_C4);
}
structIn->counter = 0;
} |
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Oct 01, 2008 5:18 am |
|
|
Perfectly possible.
The problem is that in the first function, "StructIn", is _already_ the address of the structure. You then take the address of _this_, and pass it to the second function.
Historically, it was suggested in C, that you use different nomenclature for pointers, to avoid making this mistake. So, if you call the variable in the first function (for example), ptr_to_StructIn, it becomes obvious when you go to pass it to the second, that it is already a pointer and you shouldn't use the '&'.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
rwjzownts
Joined: 19 Jul 2008 Posts: 16
|
Thanks! |
Posted: Wed Oct 01, 2008 3:18 pm |
|
|
That makes sense. I got it to work. Thanks! |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|