c[-1]?

This is a discussion on c[-1]? within the C Programming forums, part of the General Programming Boards category; Say if a function accessed this element in an array called c. What does it mean?...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    58

    c[-1]?

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

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by matsp View Post
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    c was a pointer so it did mean one element before. Thanks.

  5. #5
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by matsp View Post
    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
    Last edited by Snafuist; 04-22-2009 at 08:24 AM.
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    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. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I agree with snafuist. It should be fine and equal to *(a - 1)...

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Quote Originally Posted by Bladactania View Post
    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.
    Right 98% of the time, and don't care about the other 3%.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    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. #10
    Registered User Tanuj_Tanmay's Avatar
    Join Date
    Feb 2009
    Location
    &CProgramming
    Posts
    12
    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. #11
    Registered User Tanuj_Tanmay's Avatar
    Join Date
    Feb 2009
    Location
    &CProgramming
    Posts
    12
    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. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    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. #13
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    The memory you're writing to isn't allocated, so it's undefined.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  14. #14
    Registered User Tanuj_Tanmay's Avatar
    Join Date
    Feb 2009
    Location
    &CProgramming
    Posts
    12
    Compiler doesn't play dice.....
    Can u make out when does it give segmentation fault and when the garbage value...

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could be overwriting something important that is located in front of the array in memory?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 05:36 PM
  2. Replies: 13
    Last Post: 09-24-2008, 06:16 PM
  3. string manipulation problem
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 10-06-2005, 06:13 AM
  4. Bubble sort
    By Lionmane in forum C Programming
    Replies: 5
    Last Post: 07-09-2005, 11:30 AM
  5. Need help to understand this STL code.
    By Hulag in forum C++ Programming
    Replies: 3
    Last Post: 04-26-2005, 01:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21