negative array index

Discussion in 'C Programming' started by David Osborn, May 23, 2005.

  1. David Osborn

    David Osborn Guest

    I have a pointer "a" that points to an element in an array that is not
    the first element. I want to use a negative index to derefence the
    pointer, like so: a[-1]. Is this valid C99? If not, I can just say
    *(a-1), right?
     
    David Osborn, May 23, 2005
    #1
    1. Advertising

  2. David Osborn

    Artie Gold Guest

    David Osborn wrote:
    > I have a pointer "a" that points to an element in an array that is not
    > the first element. I want to use a negative index to derefence the
    > pointer, like so: a[-1]. Is this valid C99? If not, I can just say
    > *(a-1), right?


    Yes, as long as your pointing to a valid location in an `array'.
    [BTW a[-1] and *(a - 1) are completely equivalent; (-1)[a], though ugly,
    would also mean the same thing.]

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://it-matters.blogspot.com (new post 12/5)
    http://www.cafepress.com/goldsays
     
    Artie Gold, May 23, 2005
    #2
    1. Advertising

  3. David Osborn

    -berlin.de Guest

    David Osborn <> wrote:
    > I have a pointer "a" that points to an element in an array that is not
    > the first element. I want to use a negative index to derefence the
    > pointer, like so: a[-1]. Is this valid C99? If not, I can just say
    > *(a-1), right?


    a[ -1 ] and *(a-1) are identical, the second being what the compiler
    converts the first to (already with C89). And yes, you can access
    elements of the array also with negative indexes for 'a' as long as
    you stay within the limits of the array 'a' is pointing into.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, May 23, 2005
    #3
  4. David Osborn

    Jack Klein Guest

    On Mon, 23 May 2005 18:40:58 GMT, David Osborn
    <> wrote in comp.lang.c:

    > I have a pointer "a" that points to an element in an array that is not
    > the first element. I want to use a negative index to derefence the
    > pointer, like so: a[-1]. Is this valid C99? If not, I can just say
    > *(a-1), right?


    This is valid in any version of C, not just C99, as long as the
    pointer itself is either within the bounds of the array or points to
    one past the last element, and the combination of the pointer and the
    subscript does point into the array, not past the end and not before
    the beginning.

    Examples:

    int a[10] = { /* whatever */ };
    int i;
    b = a + 10; /* one past end */

    for (i = -10; i < 0; i++)
    {
    int x = b; /* all valid */
    }

    ....but b[-11] is invalid.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, May 24, 2005
    #4
  5. David Osborn

    Malcolm Guest

    "David Osborn" <> wrote
    >
    >I have a pointer "a" that points to an element in an array that is not the
    >first element. I want to use a negative index to derefence the pointer,
    >like so: a[-1]. Is this valid C99? If not, I can just say *(a-1), right?
    >

    The form a[-1] is allowed.
    In my opinion this is compileable gibberish rather than C, and it has to be
    *(a-1), but other people may disagree.
     
    Malcolm, May 24, 2005
    #5
  6. On Tue, 24 May 2005 06:27:18 +0000 (UTC), "Malcolm"
    <> wrote:

    >
    >"David Osborn" <> wrote
    >>
    >>I have a pointer "a" that points to an element in an array that is not the
    >>first element. I want to use a negative index to derefence the pointer,
    >>like so: a[-1]. Is this valid C99? If not, I can just say *(a-1), right?
    >>

    >The form a[-1] is allowed.
    >In my opinion this is compileable gibberish rather than C, and it has to be
    >*(a-1), but other people may disagree.
    >

    Since the two are guaranteed to have the same meaning, along with
    (-1)[a], other than aesthetics what do you use to determine gibberish?


    <<Remove the del for email>>
     
    Barry Schwarz, May 25, 2005
    #6
  7. David Osborn

    Malcolm Guest

    "Barry Schwarz" <> wrote
    >
    > Since the two are guaranteed to have the same meaning, along with
    > (-1)[a], other than aesthetics what do you use to determine gibberish?
    >

    (-1)[a] is very clearly compileable gibberish.
    A programmer who knew only a little C wouldn't have a clue what that
    construct meant, whilst an experienced programmer would find such code hard
    to read and to maintain.
    It is a question of aethetics, but that doesn't mean "trivial" or
    "unimportant".
     
    Malcolm, May 25, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. prem_eda
    Replies:
    5
    Views:
    7,868
    Pieter Hulshoff
    Oct 11, 2004
  2. karunakar
    Replies:
    0
    Views:
    5,819
    karunakar
    Feb 16, 2005
  3. matt neuburg

    Array#insert with negative index

    matt neuburg, Apr 3, 2009, in forum: Ruby
    Replies:
    4
    Views:
    135
    Ian Hobson
    Apr 4, 2009
  4. Shawn W_
    Replies:
    5
    Views:
    283
    Aldric Giacomoni
    Sep 16, 2009
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    297
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page