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

Posted on 2014-01-06
Last Modified: 2014-01-06
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

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?
Question by:naseeam
  • 2
LVL 19

Expert Comment

by:Ken Butters
ID: 39760582
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).

Author Comment

ID: 39760607
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 ?
LVL 19

Accepted Solution

Ken Butters earned 500 total points
ID: 39760629
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.


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


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.

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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…
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 difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

776 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