Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-01-06
3
Medium Priority
?
534 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

704 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