gds@atticus.Sun.COM (Greg Schechter) (01/13/90)
Does anyone know the validity of typecasting an LHS in C. For
example, I have the following:
char *ptr;
((long *)ptr)++;
That is, ptr is a pointer to a 1 byte entity and I want to increment
that pointer by the sizeof(long), so typecasting it to a (long *) and
incrementing it does work on those compilers that can handle such a beast.
My question is: Is this portable C, K&R C, ANSI C, or none of
the above?? Please respond directly to gds@Sun.COM.
Below is a program which demonstrates this:
------------
#include <stdio.h>
main()
{
char *begin = (char *)malloc(40);
char *end = begin + 40;
char *charptr;
int i;
/* count through 40 bytes one char at a time */
for (i = 0, charptr = begin; charptr < end; charptr++, i++)
printf("%d\t0x%x\n", i, charptr);
/* count through 40 bytes one long at a time */
for (i = 0, charptr = begin; charptr < end; ((long *)charptr)++, i++)
printf("%d\t0x%x\n", i, charptr);
}
-----------------
Thanks in advance,
Greg Schechter
Graphics Standards Software
Sun Microsystems