Complete.Org: Mailing Lists: Archives: freeciv-dev: January 2001:
[Freeciv-Dev] Re: (offtopic) C subscripts; was Re: Compiler-warnings
Home

[Freeciv-Dev] Re: (offtopic) C subscripts; was Re: Compiler-warnings

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: "Gaute B Strokkenes" <gs234@xxxxxxxxx>
Cc: "freeciv development list" <freeciv-dev@xxxxxxxxxxx>
Subject: [Freeciv-Dev] Re: (offtopic) C subscripts; was Re: Compiler-warnings
From: "SamBC" <sambc@xxxxxxxxxxxxxxxxxxx>
Date: Fri, 26 Jan 2001 18:43:39 -0000

----- Original Message -----
From: "Gaute B Strokkenes" <gs234@xxxxxxxxx>
To: "SamBC" <sambc@xxxxxxxxxxxxxxxxxxx>


> On Fri, 26 Jan 2001, sambc@xxxxxxxxxxxxxxxxxxx wrote:
>
>
> >
> > Not sure if you're right though...
>
> Oh, I'm right.  The above example will compile and run.
>
> Also, from the comp.lang.c FAQ:
>
> 6.11:   I came across some "joke" code containing the "expression"
>         5["abcdef"] .  How can this be legal C?
>
> A:      Yes, array subscripting is commutative in C.  The array
>         subscripting operation a[e] is defined as being identical to
>         *((a)+(e)).

That is a little strange!

>
> > I think some people are going odd on the thing about pointer
> > arithmetic/subscripts... given that an array name and a pointer are
> > the same thing,
>
> No!  However, the compiler will make implicit casts between the two
> (in most, but not all, circumstances).  Read the comp.lang.c FAQ.  I
> think it should be required reading for everyone who wants to program
> in C.

Perhaps this is a difference between C/C++ - all my learnings of C++
state that array subscripting works on pointers & pointer arithmetic
works on array names because they are, in the system, essentially the
same thing. Array out-of-bounds has to be picked up at compile-time for
definite-sized arrays, unless you use an Array object (C++). This is
just from the teaching I've had for C++, so if it's wrong, blame those
who taught me!

>
> > you get the same results, excepting location in memory, with (excuse
> > C++ code, point is the same in C)
> >
> > char* p = new char[50];
> > //initialise array
> > cout << p[20];
> > cout << *(p+20);
> >
> > as you do with
> >
> > char p[50];
> > //initialise array
> > cout << p[20];
> > cout << *(p+20);
> >
> > and both should give unexpected results (or possibly errors) with
> >
> > cout << p[-1];
> >
> > as with
> >
> > cout << *(p-1);
> >
> > as well as with
> >
> > cout << p[51];
>
> No need to go to 51.  Remember, for an array of size x > 0, the
> indices ranges from 0 through x - 1.

Said I was tired

>
> > and
> >
> > cout << *(p+51);
> >
> > Sory if someone else said this, or if I misunderstood... I'm a
> > little behind, a little out of practise, and a little tired...
>
> You're invoking undefined behaviour above, in both cases.  So I'm not
> really sure what you're trying to say.

That's what I was trying to say - I got the impression that someone (not
you) had thought p[-1] would wraparound to the end of the array, but I
may just be misunderstanding... and this is a bit TOO OT now I think...
sorry for any inconvenience caused

SamBC




[Prev in Thread] Current Thread [Next in Thread]