delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/10/12/18:41:44

Newsgroups: comp.os.msdos.djgpp
From: design AT netcom DOT com (Chris Waters)
Subject: Re: Pointers
Message-ID: <designDz6Mun.DAq@netcom.com>
Organization: Design and Delivery
References: <53oa2v$kr4 AT miracle DOT cybernex DOT net>
Date: Sat, 12 Oct 1996 21:39:59 GMT
Lines: 25
Sender: design AT netcom20 DOT netcom DOT com
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

In article <53oa2v$kr4 AT miracle DOT cybernex DOT net>,  <mrichman AT cybernex DOT net> wrote:
>Is it safe to assume that all pointer variables take up the same
>number of bytes, or is that compiler dependent.

In djgpp?  If so, why are you asking if it's compiler dependent?  If
not, why are you asking in c.o.m.djgpp?  :)

I don't believe that it is; however, the standard does guarantee that
any pointer (*except* function pointers) can safely be cast to void *,
and then back _to the same type of pointer that it originally was_.

Thus, if you want to keep a bunch of different pointers in an array, you
can make an array of void *; just be sure that you keep track of the
original type of each pointer.  Something like:

  enum ptr_type_flag { charp_f, shortp_f, intp_f, /* ... */ };

  struct tagged_ptr { enum ptr_type_flag type; void *ptr; };

  struct tagged_ptr my_array [NUM_ELEMENTS];

This should be completely portable, although it's also error prone, and
has a fair amount of overhead.  You can eliminate the overhead and just
use an array of void *, and you'll have something that should work on
_most_ platforms, but is even more error prone.
rithmetic on `void'- and Function-Pointers
===========================================

   In GNU C, addition and subtraction operations are supported on
pointers to `void' and on pointers to functions.  This is done by
treating the size of a `void' or of a function as 1.

   A consequence of this is that `sizeof' is also allowed on `void' and
on function types, and returns 1.

   The option `-Wpointer-arith' requests a warning if these extensions
are used.
-------------------------------------------------------------------------

Thirdly, your loop should look like this:

	for (int a=0;a<20;a++)
	   printf("%d\n", *(my_array + a));

This is because by definition, 
	*(<pointer> + <integer>) 
is equivalent to
	<pointer>[<integer>]
or even 
	<integer>[<pointer>].

(If you see code like this:  4["abcdefg"], then it's the same as 
 "abcdefg"[4].)

Finally, to answer the original question, is it safe to assume that 
sizeof(<a_type> *) is the same on all platforms? 

No, it is not. A pointer is something which points to some address in
RAM. On i386, i486, i586, ..., that usually means 32 bit addresses,
but it's not a long time ago when we used to program within a 16 bit
address space (i.e. sizeof pointers was 2.) 

PKE.
-- 
Pål-Kristian Engstad, Funcom Oslo AS, http://www.funcom.com/~engstad
mailto:engstad AT funcom DOT com, +47 22 42 01 02.

- Raw text -


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