Solved

How to define local static variable inside case statment block?

Posted on 2014-01-10
4
285 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

733 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