Can't use pointers nor refrences.

Has anyone had any sucess with using pointers or refrences? I thought MLAB was C '89 compleient, I thought pointers atleast were in the orignal C “release”

Pointers worked fine for me, once I got the hang of the syntax. I don’t often use C, and I find it very nonintuitive.

I also use pointers without any problems, but I’m an old C programmer.

Pointers work just fine. Here’s a simple example from the default code (in user_routines.c):

* FUNCTION NAME: Limit_Switch_Max
* PURPOSE:       Sets a PWM value to neutral (127) if it exceeds 127 and the
*                limit switch is on.
* CALLED FROM:   this file
*     Argument       Type             IO   Description
*     --------       -------------    --   -----------
*     switch_state   unsigned char    I    limit switch state
*     *input_value   pointer           O   points to PWM byte value to be limited
* RETURNS:       void
void Limit_Switch_Max(unsigned char switch_state, unsigned char *input_value)
  if (switch_state == CLOSED)
    if(*input_value > 127)
      *input_value = 127;

“unsigned char *input_value” says that “input value” is a pointer to an “unsigned char” type variable.

“*input_value > 127” and “*input_value = 127” are references to the value stored where the pointer… uh… points. :o

Later on in the same source file, you can find examples of how this function is called:

  Limit_Switch_Max(!rc_dig_in05, &pwm03);

This line passes the address of the pwm03 variable to the Limit_Switch_Max function for it to use as a pointer back to the memory location occupied by the pwm03 variable.

Does this help? If you can give us some clues about what you’re trying to do with pointers (and how much you already know or don’t know about pointers), I imagine we can be a little more helpful.

Sort of offtopic, but do you know why IFI used a pointer in the Limit_Switch_Max() function? I can’t see any good reason except possibly to show that it could be done?

They’re passing the PWM value by reference so they can update it from inside the function.

If they hadn’t, they would have to change Limit_Switch_Max to type unsigned char (instead of void) and return the value of either input_value or 127.

Or is that what you referring to?

Sorry, I should have been a bit more clear. I know they were passing it by pointer so they could change it inside the function, but I would have thought it better practice to return the value than output from inside the function. Isn’t it good practice, in general, to have functions be as close to real mathematical functions as possible (one set of inputs always maps to the same outputs and the only outputs are what the function returns)

Just a warning that references aren’t C. (Hence why they may not be working. C++ only. :))

Generally speaking, you are correct. If I or one of my team members were to write a function similar to the one Greg pointed out, we would have used a return statement and made the function of type unsigned char. However, Innovation FIRST tries to give a wide variety of examples in the default code to facilitate teams in developing their own code. This example will help some people see the syntax of a pointer in code, or help them understand one of the trickier programming topics. In most cases, you want to avoid pointers when possible.