Solved

Does typecasting what pointer points to affect pointer math?

Posted on 2008-06-10
8
245 Views
Last Modified: 2010-04-21
Assume unsigned short is 1 byte and unsigned int is 2 bytes.  Please consider following code Snippet:

void program(unsigned short *dst, unsigned short const *src, size_t num)
{
      unsigned short status=1;

      /* Now program cell-wise (i.e 8 bytes in one stretch) */
      while ((num > 0) && (status))
      {
            status = m32_program_8byte((unsigned int *)dst, (unsigned int *)src);

            num -= 8;
            dst += 8;
            src += 8;
   }
}

Pointers dst and src were pointing to unsigned short but this is modified so these pointers are pointing to unsigned int.  When pointer math is performed at the end of the function, will dst and src increase by 8 bytes or 16 bytes.

thanks.
0
Comment
Question by:naseeam
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 160 total points
ID: 21752505
src and dst will increase by sizeof(unsigned short)*8 bytes

0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 160 total points
ID: 21752527
dst = (unsigned short *) (((unsigned int *)dst)+8);
would increase it by sizeof(unsigned int)*8 bytes
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 160 total points
ID: 21752720
>Pointers dst and src were pointing to unsigned short but this is modified so these pointers are pointing to unsigned int.
They typecast was made only to arguments of the function. The type of src and dst has remain unchanged.
When you say src = src + 8, the type of src is still unsigned short *. So pointer arithmetic would be done on an unsigned short pointer ... hence it will be incremented by 8 bytes.


>status = m32_program_8byte((unsigned int *)dst, (unsigned int *)src);
This typecast applies to argument passed to this function. If this function accepts pointers to unsigned int and you were to add 8 to src inside this function, then it would have been incremented by 16 bytes since in this function, src is a pointer to unsigned int.
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 45

Expert Comment

by:sunnycoder
ID: 21752736
Basically type casting changes the type within the expression in which it has been typecasted - it does not affect the actual type of the declared variable.
0
 
LVL 84

Expert Comment

by:ozo
ID: 21752800
m32_program_8byte((unsigned int *)dst, (unsigned int *)src);
no more modifies src and dst than would
m32_program_8byte(dst+8,src+8);
0
 
LVL 40

Accepted Solution

by:
evilrix earned 180 total points
ID: 21752940
>> Basically type casting changes the type within the expression in which it has been typecasted - it does not affect the actual type of the declared variable.
Just to augment what sunnycoder has stated, when you cast a type the result is a temporary r-value of the type you are casting to, it does not modify, in anyway, the original type. In your code neither src nor dst will have their types changed by the cast that takes place in the call to m32_program_8byte. What will happen is that the temporary and anonymous r-values that result from these casts will be passed to the function and once this line of code completes the temporary r-values are destroyed leaving src and dst as they were.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21752945
>> Just to augment what sunnycoder
oops, and ozo -- sorry ozo for missing you out :)
0
 
LVL 1

Author Closing Comment

by:naseeam
ID: 31465785
Excellent responses.  Problem solved.  Clear and detailed explanations.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Picking random number 8 178
undefined reference to `bswap_128' 9 171
Want to delete all my personal data 13 146
Windows Updates failing due to Diskpart not configured correctly 8 130
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

861 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