| Is it possible to add one or two bytes to a longword pointer?

Previous | Memory, C Variables, and Pointers | Next

Q: | Is it possible to add two bytes or even one byte to a longword pointer? I am using a longword pointer to write longwords to the screen, and I need to add 30 bytes to it to get to the next line. However, I can only add multiples of four bytes to it...

A:

It is possible using typecasting:

    ptr = (long*)((char*)ptr + 30);

Don't be afraid, the compiler will generate just addition: everything other is just to satisfy type checking conventions. Or alternatively, you can use even simpler form:

    (char*)ptr += 30;

Although such form is not requested to work in ANSI C standard, the most of compilers (including TIGCC) will accept this.