delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/07/29/10:41:00

Message-Id: <m0z1XMJ-000S4AC@inti.gov.ar>
Comments: Authenticated sender is <salvador AT natacha DOT inti DOT gov DOT ar>
From: "Salvador Eduardo Tropea (SET)" <salvador AT inti DOT gov DOT ar>
Organization: INTI
To: nikki AT kki DOT net DOT pl (Inquisitor Nikodemus), djgpp AT delorie DOT com
Date: Wed, 29 Jul 1998 11:47:25 +0000
MIME-Version: 1.0
Subject: Re: GCC and pointers : QUESTION
In-reply-to: <35bf1ff4.1246159@news.icm.edu.pl>

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019