[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: C's pointer arithmetic (Was Re: Setting effective UID for a shell script)



On Thu, May 16, 2002 at 06:13:08PM +0930, Tom Cook wrote:
> On  0, Perceval Anichini <anichi_p@epita.fr> wrote:
> [snip]
> > > Moreover, argv + sizeof (argv[1]) is equal to argv[4] (as sizeof (char*) =
> > 4) ...
> > 
> > > No.  If that were so then you could not access the list of arguments
> > > to a main function as argv[0], argv[1], argv[2] etc.  The compiler
> > > knows that the type of argv is char**, or char*[], an array of
> > > pointers to chars, and it knows that sizeof( char* ) is 4 (on the
> > > platforms we're talking about).  It is almost an axiom that
> > > argv[1] == argv[0] + sizeof( argv[0] ), since this is the address of
> > > the next element in the array.  So argv[4] = argv + 4*sizeof( argv[1]).
> > 
> > Wrong. We are talking about pointers, not about integers nor unsigned.
> > When you write
> >  argv + 1, the compiler will understand : compute the address
> > of argv, and add one time the size of the type which is pointed by argv.
> > I remember to you that argv[1] = argv + 1. Brackets are only syntactic
> > sugar, which allows us not seeing we are dealing with pointer arithmetic.
> 
> Is that so?  I didn't know that.  My understanding of the situation
> was that
> 
> argv + 4
> 
> was equivalent to
> 
> ((int)argv) + 4,
> 
> not
> 
> argv + (char*)4.
> 
> But I think that I learnt that A long long time ago, on a compiler far
> far away...

All this reminds me why I try to avoid pointer arithmetic except in its
simplest forms. I would just use

    &(argv[<whatever>])

so that later on, I can understand what I meant, and why it says what I
meant.

-- 
Note that I use Debian version 3.0
Linux mus 2.4.17mvz4 #1 Fri Mar 15 23:30:15 CET 2002 i686 unknown

Matijs van Zuijlen

    ... designed to fill holes or cracks of not more than two cubic vims.
			    -- Robert Sheckley, Untouched by Human Hands

Attachment: pgpsxEd2y4ZDH.pgp
Description: PGP signature


Reply to: