Solved

How to define local static variable inside case statment block?

Posted on 2014-01-10
4
286 Views
Last Modified: 2014-01-10
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 ?
0
Comment
Question by:naseeam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39772245
What errors do you get?  It should work that way, maybe encloing the block in braces helps, e.g.

case Flag_ThreeParamReg: { // <--- here...
      /* 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;
			}
                } // <--- and here
		break;
                                  

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 39772251
Oh: Ahnd maybe try to initialize the static variables...
0
 
LVL 1

Author Comment

by:naseeam
ID: 39772358
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 39772388
Aaaah, I was already wondering.... If you only need them inside the 'switch()' statement, that's fine.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question