Solved

How to point pointer to word(2 bytes) after pointer subtraction ?

Posted on 2014-01-06
3
482 Views
Last Modified: 2014-01-06
void 
ModbusPoll::processSpecialParam(ParameterDef pTable[], char * val, unsigned short * ptr_buffIndex)
{
/* some code here */
case Flag_TwoParamReg:
  unsigned short modbus_reg_40045_save_value;
  modbus_reg_40045_save_value =  * ( (unsigned short *) val ) ;
  * ( ( unsigned short * ) val - 1 ) = 0x0003 & modbus_reg_40045_save_value ;

Open in new window


My doubt is if I'm correctly typecasting val to point to 2 bytes, then subtract one from val.
The pointer that is result of subtraction needs to point to 2 bytes so 2 bytes can be
stored.

The code above seems to store two bytes in location pointed to by computed address but how does it know to strore two bytes when I don't explicitly typecast resultant pointer to unsigned short?
0
Comment
Question by:naseeam
  • 2
3 Comments
 
LVL 19

Expert Comment

by:Ken Butters
Comment Utility
Hopefully I'm answering what you are asking.

When you increment or decrement a pointer, you are pointing to the previous or next element of the array that the pointer is pointing to.  You are not incrementing or decrementing the address the pointer holds.

Therefore if you subtract 1 from a pointer... it will point to the previous element in the array.  If that element was an unsigned short... that would be 2 bytes (on any machine that I know of anyway).
0
 

Author Comment

by:naseeam
Comment Utility
There is no way of knowing the size of previous element.  Pointer was passed to the function as            char * val

All we know is that pointer points to char.  When we typecast it to unsigned short, then it point to 2 bytes.  When we subtract one from the pointer, how do we know how many bytes the computed pointer points to ?
0
 
LVL 19

Accepted Solution

by:
Ken Butters earned 500 total points
Comment Utility
If you cast the pointer to unsigned short....

Then it doesn't matter what the it is really pointing to... because you told the compiler that the pointer was pointing to an array of unsigned shorts.

so if you subtract 1... then it will back up 2 bytes... to point to the previous unsigned short.  Even though it may not really be an unsigned short... you told the compiler that it is in fact an unsigned short.... so that is how it will be treated.

Suppose you have this... (even if the pointer is pointing to a character array) if you cast it to unsigned pointer... you are telling the compiler that you have the following situation... and it will believe you.

short[1]
short[2]<-----<pointer>
short[3]

If you subtract 1 from the pointer the result will be this:

short[1]<-----<pointer>
short[2]
short[3]

The math on the pointer is computed based on what type of data you say the pointer is pointing to.... it has nothing to do with the underlying data.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 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

13 Experts available now in Live!

Get 1:1 Help Now