Solved

How to define local static variable inside case statment block?

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

856 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