[Freeciv-Dev] Re: (offtopic) C subscripts
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
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!
- [Freeciv-Dev] Re: (offtopic) C subscripts; was Re: Compiler-warnings, (continued)
- [Freeciv-Dev] Re: (offtopic) C subscripts; was Re: Compiler-warnings, Reinier Post, 2001/01/26
- [Freeciv-Dev] Re: (offtopic) C subscripts; was Re: Compiler-warnings, Gaute B Strokkenes, 2001/01/27
- [Freeciv-Dev] Re: (offtopic) C subscripts; was Re: Compiler-warnings, Raimar Falke, 2001/01/27
- [Freeciv-Dev] Re: (offtopic) C subscripts; was Re: Compiler-warnings, Gaute B Strokkenes, 2001/01/27
- [Freeciv-Dev] Re: (offtopic) C subscripts; was Re: Compiler-warnings, Raimar Falke, 2001/01/27
- [Freeciv-Dev] Re: (offtopic) C subscripts; was Re: Compiler-warnings, Gaute B Strokkenes, 2001/01/27
[Freeciv-Dev] Re: (offtopic) C subscripts; was Re: Compiler-warnings, Gaute B Strokkenes, 2001/01/25
|
|