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: freeciv development list <freeciv-dev@xxxxxxxxxxx>
Subject: [Freeciv-Dev] Re: (offtopic) C subscripts
From: Dirk Stoecker <stoecker@xxxxxxxxxxxxxx>
Date: Sat, 27 Jan 2001 11:19:12 +0100 (MET)

Hi,

> > int
> > main (int argc, char *argv[])
> > {
> >   printf("The third letter of the alphabet is %c.\n", 2["ABC"]);
> > }
> 
> Not sure if you're right though... I think some people are going odd on

He is right and compilers do not give a warning on this  :-(

> the thing about pointer arithmetic/subscripts... given that an array
> name and a pointer are the same thing, you get the same results,
> excepting location in memory, with (excuse C++ code, point is the same
> in C)
> 
> 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;

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

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

Ciao
 ____  _ _  ____  _ _    _ _  ____
|    |  |  |    |  | \  / |  |    | the cool Gremlin from Bischofswerda
|  __   |   ____|  |  \/  |  |    | WWW: http://home.pages.de/~stoecker/
|    |  |  |       |      |  |    | PGP key available on www page.
|____| _|_ |____| _|_    _|_ |____| I hope AMIGA never ends to make fun!




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