Multidimensional Arrays

This is a discussion on Multidimensional Arrays within the C++ Programming forums, part of the General Programming Boards category; Whenever I try running this program it always gives 0x441020. It is supposed to output 20. The compiler I'm using ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    7

    Multidimensional Arrays

    Whenever I try running this program it always gives 0x441020. It is supposed to output 20. The compiler I'm using is code blocks. The program does return 0. So if someone could point out what is wrong with this program that would be much appreciated.
    Code:
    #include <iostream>
    using namespace std;
    
    int array[2][4] =
    {
        {1, 2, 3, 4},
        {10, 20, 30, 40}
    };
    int main()
    
    {
        std::cout << array[2, 2] << "\n";
        cin.get();
        return(0);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    array[2][4]
    Code:
    array[2, 2]
    Compare and contrast.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    7
    Oh wow, that was easy. I forgot that C++ starts at 0 and not 1.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That's not it. (Well, yes, the first 2 would be out of range if your code did what you think.)

    There is the comma operator which makes this code compile but not do what you think.
    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).

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    7
    Yeah that also, thanks.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    and one more thing. you used std::cout when you had already added std namespace at the begining of your code.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by Cherry65
    and one more thing. you used std::cout when you had already added std namespace at the begining of your code.
    That is perfectly fine, though rather redundant in this case since there is nothing else named cout.
    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. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:16 PM
  2. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 09:35 PM
  3. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  4. Multidimensional arrays in Korn shell
    By Yasir_Malik in forum Tech Board
    Replies: 3
    Last Post: 04-11-2004, 02:16 PM
  5. Adding multidimensional arrays
    By newbie2C++ in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2001, 03:05 PM

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