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

[Freeciv-Dev] Re: (offtopic) C subscripts

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: "Dirk Stoecker" <stoecker@xxxxxxxxxxxxxx>, <freeciv-dev@xxxxxxxxxxx>
Subject: [Freeciv-Dev] Re: (offtopic) C subscripts
From: "SamBC" <sambc@xxxxxxxxxxxxxxxxxxx>
Date: Sat, 27 Jan 2001 13:36:20 -0000

----- Original Message -----
From: "Dirk Stoecker" <stoecker@xxxxxxxxxxxxxx>


> >
> > char* p = new char[50];
>
> You misunderstood the point: Lets change this line to 2 new lines:
> char *a = new char[100];
> char *p = a + 20;

Okay, now I understand the point, I have another to make - I was always
taught that, while they are the same, you should try and keep to
pointers and arrays seperate, and use pointer arithmetic on arrays to
make iterative processes easier. If you make a pointer to anywhere in
the array other than a[0], you shouldn't subscript it, but you can use
it to move around with p+=n, p-=n,++p,p++, etc etc...

Note that most of those 'cans' are more literally 'shoulds', but I stick
ot how I was taught unless it stops me doing something...

>
> > //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);
>
> Although we use negative subscripts now, we access valid array entries
and
> don't cause segfaults.

That just seems plain ugly...

>
> > Sory if someone else said this, or if I misunderstood... I'm a
little
> > behind, a little out of practise, and a little tired...
>
> Negative offsets are really ugly and I always try to avoid them, but
it is
> not always possible.

Ugly, and potentially problematic if you don't check the whole piece
when altering some array code. Unlikely to cause problems, but
possible...


SamBC




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