Solved

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

Posted on 2014-01-06
3
513 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
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).
0
 
LVL 1

Author Comment

by:naseeam
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 ?
0
 
LVL 19

Accepted Solution

by:
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.

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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.

820 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