Thread: Multidimensional Arrays

  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,336
    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
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 10: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, 04:05 PM