Solved

How to define local static variable inside case statment block?

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

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now