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

x
?
Solved

Does typecasting what pointer points to affect pointer math?

Posted on 2008-06-10
8
Medium Priority
?
252 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 85

Assisted Solution

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

0
 
LVL 85

Assisted Solution

by:ozo
ozo earned 640 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 640 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 85

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 720 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 and use switch statements in the C programming language.

972 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