Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. array[-1]

array[-1]

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
3 Posts 3 Posters 13 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zerg
    wrote on last edited by
    #1

    this is certainly beyond my knowledge to learn that array[-1] is valid in some cases. i am shocked, stunned and desparate ! i am now examing an existing project and i am suspecting this is the cause of all evils. but now somebody said this is perfectly legal. okay okay.. let me draw the picture bigger. there is this array of struct defined like this.. struct someStruct array[100] and i am assigning like this array[-1].field1 = something. so so..is this so called access the return address or read parameter directly ?? arghhh !!! what am i talking about ?? some1 pls resurrect me !!

    M J 2 Replies Last reply
    0
    • Z Zerg

      this is certainly beyond my knowledge to learn that array[-1] is valid in some cases. i am shocked, stunned and desparate ! i am now examing an existing project and i am suspecting this is the cause of all evils. but now somebody said this is perfectly legal. okay okay.. let me draw the picture bigger. there is this array of struct defined like this.. struct someStruct array[100] and i am assigning like this array[-1].field1 = something. so so..is this so called access the return address or read parameter directly ?? arghhh !!! what am i talking about ?? some1 pls resurrect me !!

      M Offline
      M Offline
      Mike Dunn
      wrote on last edited by
      #2

      It's my understanding that a[i] is identically equivalent to *(a + i). No restriction is made on the sign of i, so array[-1] is syntactically fine. However, in your code example:

      struct someStruct array[100];
      array[-1].field1 = something;

      That is accessing outside the bounds of the array. Bad move. It's just as bad as doing "array[2000].field1=xyz;"

      1 Reply Last reply
      0
      • Z Zerg

        this is certainly beyond my knowledge to learn that array[-1] is valid in some cases. i am shocked, stunned and desparate ! i am now examing an existing project and i am suspecting this is the cause of all evils. but now somebody said this is perfectly legal. okay okay.. let me draw the picture bigger. there is this array of struct defined like this.. struct someStruct array[100] and i am assigning like this array[-1].field1 = something. so so..is this so called access the return address or read parameter directly ?? arghhh !!! what am i talking about ?? some1 pls resurrect me !!

        J Offline
        J Offline
        Jesse Ezell
        wrote on last edited by
        #3

        It would only be useful in a small number of cases. Say, if you wrote a peice of code like this: char text[100]; char *ref = text[1]; if(*ref == ref[-1]) return FIRST_EQUALS_SECOND; However, I don't see why you would want to do that in any normal case. ================== The original message was: this is certainly beyond my knowledge to learn that array[-1] is valid in some cases.
        i am shocked, stunned and desparate !
        i am now examing an existing project and i am suspecting this is the cause of all evils.
        but now somebody said this is perfectly legal.
        okay okay..
        let me draw the picture bigger.
        there is this array of struct defined like this..
        struct someStruct array[100] and i am assigning like this
        array[-1].field1 = something.

        so so..is this so called access the return address or read parameter directly ??
        arghhh !!! what am i talking about ?? some1 pls resurrect me !!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups