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

passing a struct through one function into another

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



Joined: 19 Jul 2008
Posts: 16

View user's profile Send private message

passing a struct through one function into another
PostPosted: Tue Sep 30, 2008 5:28 pm     Reply with quote

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;
}
Ttelmah
Guest







PostPosted: Wed Oct 01, 2008 5:18 am     Reply with quote

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
rwjzownts



Joined: 19 Jul 2008
Posts: 16

View user's profile Send private message

Thanks!
PostPosted: Wed Oct 01, 2008 3:18 pm     Reply with quote

That makes sense. I got it to work. Thanks!
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