delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/10/15/22:27:58

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Pointers
Date: Tue, 15 Oct 1996 18:08:51 -0700
Organization: Alcyone Systems
Lines: 36
Message-ID: <326435A3.4B770109@alcyone.com>
References: <53oa2v$kr4 AT miracle DOT cybernex DOT net> <53oql4$qg7 AT news DOT ox DOT ac DOT uk>
NNTP-Posting-Host: newton.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

George Foot wrote:

> #define POINTER_SIZE sizeof(*void)

You mean sizeof(void *).

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

This is fundamentally wrong for several reasons.

First, pointer arithmetic doesn't work that way -- the type of the pointer
is taken into account, so my_array + i is the same as &my_array[i]; it
automatically computes the size of the pointer in question.

Second, this implicitly assumes (given your incorrect understanding of
pointer arithmetic) that all pointers are the same size, which is not only
not guaranteed by the ANSI C standard, but is in fact what the original
poster was _asking about in the first place_!

> then when you compile on a different platform the multiplier will be
> correctly adjusted.
> 
> Of course, it's much simpler to write "my_array[a]", but I presume you
> don't want to do this for some reason.

The code you posted isn't even equivalent to that, much less does it
address any portability issues.

-- 
                             Erik Max Francis | max AT alcyone DOT com
                              Alcyone Systems | http://www.alcyone.com/max/
                         San Jose, California | 37 20 07 N 121 53 38 W
                                 &tSftDotIotE | R^4: the 4th R is respect

- Raw text -


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