1. ## c[-1]?

Say if a function accessed this element in an array called c. What does it mean?

2. It is technically undefined, if c is an array. If c is a pointer INTO an array, then I think it is still undefined by the standard, but it's possible that it "works" correctly - getting the element BEFORE c.

--
Mats

3. Originally Posted by matsp
It is technically undefined, if c is an array. If c is a pointer INTO an array, then I think it is still undefined by the standard, but it's possible that it "works" correctly - getting the element BEFORE c.

--
Mats
Why would it be undefined?
It should be equivalent to *(c-1)

if c-1 is valide pointer (still pointing to the inside of the array) - you can dereference it

4. c was a pointer so it did mean one element before. Thanks.

5. Originally Posted by matsp
It is technically undefined, if c is an array. If c is a pointer INTO an array, then I think it is still undefined by the standard, but it's possible that it "works" correctly - getting the element BEFORE c.
Citing from my copy of the standard:

J.2 Undefined behavior

The behavior is undefined in the following circumstances:

[...]

- Addition or subtraction of a pointer into, or just beyond, an array object and an integer type produces a result that does not point into, or just beyond, the same array object (6.5.6).
I read this as "if c[-1] is an object of the same array as the one that contains c[0], then it's ok". Besides, the subscript operator in E1[E2] is defined as (*((E1) + (E2))) where E1 is a pointer and E2 is an integer.

Hence, the following should be ok:

Code:
```int a[8];
int *c = a + 4;
if(c[-1] == a[3])
puts("works");```
Greets,
Philip

6. In the case you site there then c[-1] is technically a valid statement. But if you have

Code:
`int c[8];`
then c[-1] would return an unknown value since that address isn't part of the array c. I think this is more in the nature of the original question.

So while c[-1] isn't undefined, except in the case you refer to, it is an unknown value.

7. I agree with snafuist. It should be fine and equal to *(a - 1)...

In the case you site there then c[-1] is technically a valid statement. But if you have
Code:
`int c[8];`
then c[-1] would return an unknown value since that address isn't part of the array c.
The expressions c[-1] doesn't just yield an "unknown value". In this case (since the first element of c is c[0]) the expression c[-1] or, equivalently, *(c-1) yields undefined behaviour.

9. I don't think that is considered undefined behavior. c[-1] returns the "value" stored in the address that is one before c[0].

Although, I guess that if c[0] is stored on address "0" or the lowest possible address, then c[-1] would generate an error. Or of c[0] is one address after the end of system protected memory.

I append my original statement. c[-1] yields undefined behavior.

10. why will C compiler give error if you try to access a memory.....
the only differnece here is you haven't stored any value there...
it will definitely print some value...but it will be a GARBAGE.

Python....whereas prints the value of last element of array
...suppose i have array of 5 elements then a[-1] is equivalent to a[4]
...but thats in PYTHON

11. But if store some value there...it will print it
example
Code:
```#include<stdio.h>

int main()
{
int c[] = {2,5,6,8,3,6};
c[-1] = 57;
printf("%d",c[-1]);
return 0;
}```
Code:
```OUTPUT:
57```

12. Yes you can do that, but you are likely to get a seg fault or other unexpected behavior if you do something like that.

Just because something works SOMETIMES doesn't mean it will work ALL times.

13. The memory you're writing to isn't allocated, so it's undefined.

14. Compiler doesn't play dice.....
Can u make out when does it give segmentation fault and when the garbage value...

15. You could be overwriting something important that is located in front of the array in memory?