array[-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 !!
-
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 !!
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;"
-
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 !!
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 !!