Array access

This is a discussion on Array access within the C Programming forums, part of the General Programming Boards category; I dont know if the following is a bug or something I am doing wrong. I have compiled this with ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    145

    Array access

    I dont know if the following is a bug or something I am doing wrong. I have compiled this with Codeblock and MINGW.

    The following code:

    Code:
    #include <stdio.h>
    
    const int EV[17] = {-30,-27-24,-21,-18,-15,-12,-9,-6,-3,0,3,6,9,12,15,18};
    const int ET[3] = {1,2,3};
    
    int main(void)
    
    {
      int x = 16;
      printf("EV[%d] = %d\n",x,EV[x]);
      x = 2;
      printf("ET[%d] = %d\n",x,ET[x]);
    }
    is showing this output:

    Code:
    EV[16] = 0
    ET[2] = 3
    Why EV[16] is showing 0 and not 18 ??

    p.s.: I have also compile this with tcc with the same output.
    Last edited by Kempelen; 05-06-2009 at 02:29 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    ... -30,-27-24, ...
    Would there be a comma missing on that line?

    --
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,606
    Because the second element of EV is -27-24=-51, the third is -21, ..., the second last is 18 and the last is 0.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    145
    Quote Originally Posted by matsp View Post
    Code:
    ... -30,-27-24, ...
    Would there be a comma missing on that line?

    --
    Mats
    Oh my god. I have reviewed the code a few times and didn't noted anything. I don't know why I use glasses......

    thx

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by Kempelen View Post
    I dont know if the following is a bug or something I am doing wrong. I have compiled this with Codeblock and MINGW.

    The following code:

    Code:
    #include <stdio.h>
    
    const int EV[17] = {-30,-27-24,-21,-18,-15,-12,-9,-6,-3,0,3,6,9,12,15,18};
    const int ET[3] = {1,2,3};
    
    int main(void)
    
    {
      int x = 16;
      printf("EV[%d] = %d\n",x,EV[x]);
      x = 2;
      printf("ET[%d] = %d\n",x,ET[x]);
    }
    .
    main should return 0 at last.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,606
    Quote Originally Posted by BEN10
    main should return 0 at last.
    That is optional with respect to the 1999 edition of the C standard.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by laserlight View Post
    That is optional with respect to the 1999 edition of the C standard.
    Thanks for the information, but how can it be optional because any function which returns int should actually do that, otherwise it's return type will be void. Seriously these types of things in C confuses me a lot.
    Last edited by BEN10; 05-06-2009 at 05:05 AM.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,606
    Quote Originally Posted by BEN10
    Thanks for the information, but how can it be optional because any function which returns int should actually do that, otherwise it's return type will be void.
    Special case for the main function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Access array from class
    By parad0x13 in forum C++ Programming
    Replies: 6
    Last Post: 03-18-2009, 08:12 AM
  3. How to access a dynamic array inside a std::set ?
    By IndioDoido in forum C++ Programming
    Replies: 16
    Last Post: 11-04-2007, 04:27 PM
  4. access violation in int array
    By George2 in forum C Programming
    Replies: 2
    Last Post: 08-02-2007, 11:28 PM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM

Tags for this Thread


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