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

x
?
Solved

Does typecasting what pointer points to affect pointer math?

Posted on 2008-06-10
8
Medium Priority
?
251 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
[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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 84

Assisted Solution

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

0
 
LVL 84

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 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's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-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.

661 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