Mail Archives: djgpp/1998/07/29/10:00:43
> During writing a function I noticed that operations - such as addition
> or substraction - on pointers of different types than char didn't
> behave as I expected them to. Eg. adding 2 to the the pointer :
> short *pointer_to_short ;
> resulted in 4 byte offset,not 2 byte. Same for ints etc.
> So the question is : is it really a fact,that gcc's pointer math
> depends on type rather than raw bytes ?
This is the way the C language is supposed to work; it has nothing to
do with compiler-specific stuff. If you have a pointer to a short,
and you add one to it, you get a pointer to the next short. That's
the way pointer arithmetic is *supposed* to work.
If you really want to adjust it on a byte-by-byte basis, you'll need
to cast it to a pointer to bytes (chars), but you really shouldn't do
that for normal pointer math, because it makes the code harder to
read.
- Raw text -