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

Tom
-- 
Tom Cook
Information Technology Services, The University of Adelaide

"My advice to you is to get married: If you find a good wife, you will be happy; if not, you will become a philosopher."
	- Socrates

Get my GPG public key: https://pinky.its.adelaide.edu.au/~tkcook/tom.cook-at-adelaide.edu.au

Attachment: pgpYTSfuQyBOl.pgp
Description: PGP signature


Reply to: