Solved

Does typecasting what pointer points to affect pointer math?

Posted on 2008-06-10
8
242 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
Comment Utility
src and dst will increase by sizeof(unsigned short)*8 bytes

0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 160 total points
Comment Utility
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
Comment Utility
>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
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
>> 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
Comment Utility
>> Just to augment what sunnycoder
oops, and ozo -- sorry ozo for missing you out :)
0
 

Author Closing Comment

by:naseeam
Comment Utility
Excellent responses.  Problem solved.  Clear and detailed explanations.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now