www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2003/01/18/17:01:28

Message-ID: <3E298608.2381784A@acm.org>
From: Eric Sosman <esosman AT acm DOT org>
X-Mailer: Mozilla 4.72 [en] (Win95; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: each address in memory "holds" 32 bit ?
References: <b0cgvq$ad2$1 AT news DOT online DOT de>
Lines: 41
Date: Sat, 18 Jan 2003 21:49:55 GMT
NNTP-Posting-Host: 12.91.6.22
X-Complaints-To: abuse AT worldnet DOT att DOT net
X-Trace: bgtnsc05-news.ops.worldnet.att.net 1042926595 12.91.6.22 (Sat, 18 Jan 2003 21:49:55 GMT)
NNTP-Posting-Date: Sat, 18 Jan 2003 21:49:55 GMT
Organization: AT&T Worldnet
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Lars Hansen wrote:
> 
> #include "stdio.h"
> #include "string.h"
> 
> int main()
>  {
>   int i=10;
>   int x[10]={0,1,2,3,4,5,6,7,8,9};
>   memmove(x+2,x+3,sizeof(int)*7);
>   while(i--) printf("%d,",x[i]);
>  }
> 
> outputs  9,9,8,7,6,5,4,3,1,0
> 
> the same version with  memmove(x+sizeof(int)*2,x+sizeof(int)*3,sizeof(int)*7);
> 
> outputs  0,0,7,6,5,4,3,2,1,0
> 
> So is each address a 32 bit "place" or does the compiler "add" sizeof(int)*
> automatically?

    The latter.  Pointer arithmetic in C always takes into account
the size of the pointed-to object.  Re-read your introductory C text.

> I had expect x to be an address (so having a range of an (long) int) and
> adding an int increases the address by the value of the int so if the next
> memory object I want is 2 bytes further away I have to add 2 ?

    I am not sure what you are asking, or what concept is giving you
trouble.  `x' is an array of ten `int' objects x[0], x[1], ..., x[9].
In most contexts, writing `x' in an expression produces a pointer to
the first element of the array; `x' and `&x[0]' are equivalent (the
exceptions: when used as the operand of the `sizeof' or unary `&'
operators).  Thus, `x+1' is the same as `&x[0]+1', which is the same
as `&x[1]'.  Again, all this is beginner-level C; your textbook or
reference manual surely explains it.

-- 
Eric Sosman
esosman AT acm DOT org

- Raw text -


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