Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-01-06
3
Medium Priority
?
540 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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.

877 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