naseeam
asked on
Does typecasting what pointer points to affect pointer math?
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((unsigne d 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.
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((unsigne
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
m32_program_8byte((unsigne d int *)dst, (unsigned int *)src);
no more modifies src and dst than would
m32_program_8byte(dst+8,sr c+8);
no more modifies src and dst than would
m32_program_8byte(dst+8,sr
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> Just to augment what sunnycoder
oops, and ozo -- sorry ozo for missing you out :)
oops, and ozo -- sorry ozo for missing you out :)
ASKER
Excellent responses. Problem solved. Clear and detailed explanations.