Solved

Does typecasting what pointer points to affect pointer math?

Posted on 2008-06-10
8
244 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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 is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 opening and reading files in the C programming language.

770 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