1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Gosh ! 2D int array

Discussion in 'C++' started by trapeze, Sep 8, 2007.

  1. trapeze

    trapeze New Member

    Joined:
    Sep 7, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    #include <iostream>
    using namespace std;

    int main()
    {

    int a[2][13] ={
    {90, 23, 26, 31, 50, 71, 39, 31, 61, 80, 11, 20, 41},
    {50, 31, 29, 31, 50, 31, 30, 61, 34, 70, 38, 90, 41},
    };

    cout << *a[2][8] <<endl;
    return 0;
    }
    -----------
    my Dev-C++ says ... invalid type argument of `unary *'
    if i use cout << a[2][8] <<endl; , it says 207744

    why is this ? plz help~~~~ thx.
    :(
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    You don't have any value at 2,8 and so its saying you as some garbage value but the solution is not to use the * operator as that is only applicable when you have some address.
     
  3. rick

    rick New Member

    Joined:
    Nov 16, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0

    firstly since you have declared your array as a[2][13] this means that you have only two 1d arrays that can individually hold upto 13 elements and you must know that array indexing starts from 0 not 1, thus when u say a[2][8] you are saying that give me the element at position 8th in my third 1d array(which is not there) and since there are no bounds checking performed in c++ you get a garbage value instead of that you should just say a[1][8] to get the desired value and there is no need to use the indirection operator. Also if you do want to use the indirection operator the correct way is *(*(a+1)+8)
     

Share This Page