Link to home
Start Free TrialLog in
Avatar of appleby
appleby

asked on

Best way to implement a 2d array of ints?

I want to create a simple array of arrays of ints which will serve as a type of lookup table.  It will be set up once and not change during execution.  First thoughts were just to define as such:

int mytable[5][10];

in the header file.  This gives an error:  member could not be initialized.

How do I use this correctly, and is there a better way to create this table?  I considered using some other standard class but since my table size and all values will be known at compile time, I didn't see any compelling reason to.  I don't know enough to be sure though.  Suggestions?

The items in the table will be used for reference only, no arithmetic or anything being performed.

Thanks!
Avatar of Crius
Crius

Your declaration is correct. Is there a possibility it's a different variable that has a member that can not be initialized?

What else are you trying to do with the variable? Could you show me the method you are using to initialize the variable?
Avatar of jkr
This should work & does work, e.g.:

class C {
    public:

    int i[5][10];
};

Can you show your code?
Avatar of appleby

ASKER

Thanks, you are both correct, it does work.  I had forgotten that at one point I was thinking that I should make it const so there would be no question that it was not to be modified during execution, and I left the const modifier before the declaration.  I didn't notice the const when I looked at the declaration after that.

I'd still like to know whether this is the best way to implement the functionality.  It seems like overkill to do anything more since I know the size and value of the entire table.  Comments?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of appleby

ASKER

Thanks, you are both correct, it does work.  I had forgotten that at one point I was thinking that I should make it const so there would be no question that it was not to be modified during execution, and I left the const modifier before the declaration.  I didn't notice the const when I looked at the declaration after that.

I'd still like to know whether this is the best way to implement the functionality.  It seems like overkill to do anything more since I know the size and value of the entire table.  Comments?

Thanks!
Avatar of appleby

ASKER

Sorry about the double comment - reloaded the wrong page.  :p
Avatar of appleby

ASKER

Thank you both for your help.  Crius, I will be asking in Community Support for an award of 50 points to you as well for having the first correct answer to the first part of my problem.

appleby
Thanks. :)