Link to home
Start Free TrialLog in
Avatar of naseeam
naseeamFlag for United States of America

asked on

How to define local static variable inside case statment block?

case Flag_ThreeParamReg:
      /* Three parameters are in One Modbus register.  Specifically, Modbus
         Register 40057 bits 0-1 contain parameter 2807, bits 2-3 contain
		     parameter 2820, and bit 4 contains parameter 2821. */
		  /* keeps track of number of times we enter this case */
		  static unsigned char count = 1;
		  static unsigned short modbus_reg_40057_save_value;
		  if (count == 1)
			{
				/* save regsiter 40057 in variable reg_40057_saved because this
				   register contains two other parameters. */
				modbus_reg_40057_save_value = *((unsigned short *)val);
				/* extract bits 0-1 from buffer and store back in the buffer */
				*((unsigned short *)val) = 0x0003 & modbus_reg_40057_save_value;
				count++;
			}
			else if (count == 2)
		  {
				/* extract bits 2-3 from buffer and store back in the buffer */
				*((unsigned short *)val - 1) = ((0x000C & modbus_reg_40057_save_value) >> 2);
				/* decrement buffer index so value is fetched from previous register
				   entry */
				*ptr_buffIndex = *ptr_buffIndex - 1;
				count++;
			}
			else if (count == 3)
			{
				/* extract bit 4 from buffer and store back in the buffer */
				*((unsigned short *)val - 1) = ((0x0010 & modbus_reg_40057_save_value) >> 4);
				/* decrement buffer index so value is fetched from previous register
				   entry */
				*ptr_buffIndex = *ptr_buffIndex - 1;
				/* All Three parameters have been processes, reset the counter */
				count = 1;
			}
		break;

Open in new window


Are the two local static variables above defined correctly?  Do they have scope from where they are defined to break statement ?
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh: Ahnd maybe try to initialize the static variables...
Avatar of naseeam

ASKER

I don't get any errors.  I wanted to limit the scope of variables to within the case statement.  I'll enclose the block in braces.  Otherwise, if I use the varialbes outside the case statement but within the switch statement, the variable will be in scope.
Aaaah, I was already wondering.... If you only need them inside the 'switch()' statement, that's fine.