Mail Archives: djgpp/1998/07/29/10:41:00
nikki AT kki DOT net DOT pl (Inquisitor Nikodemus) wrote:
> 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 ?
Yes, in fact C pointer arithmetic is like you say. Why? because the following
are equivalent:
a[1]
and
*(a+1)
That's true even if a is an structure.
If you really want to make byte adjustements with pointers cast the pointers
to char:
long *a;
....
a=(long *)((char *)a+1);
This will increase a in 1, of course normally "a" won't point to a valid
value.
SET
------------------------------------ 0 --------------------------------
Visit my home page: http://set-soft.home.ml.org/
or
http://www.geocities.com/SiliconValley/Vista/6552/
Salvador Eduardo Tropea (SET). (Electronics Engineer)
Alternative e-mail: set-soft AT usa DOT net set AT computer DOT org
ICQ: 2951574
Address: Curapaligue 2124, Caseros, 3 de Febrero
Buenos Aires, (1678), ARGENTINA
TE: +(541) 759 0013
- Raw text -