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

Date: Wed, 29 Jul 1998 09:59:13 -0400 (EDT)
Message-Id: <199807291359.JAA15388@delorie.com>
From: DJ Delorie <dj AT delorie DOT com>
To: nikki AT kki DOT net DOT pl
CC: djgpp AT delorie DOT com
In-reply-to: <35bf1ff4.1246159@news.icm.edu.pl> (nikki@kki.net.pl)
Subject: Re: GCC and pointers : QUESTION

> 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 -


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