• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

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!
0
appleby
Asked:
appleby
  • 4
  • 2
  • 2
1 Solution
 
CriusCommented:
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?
0
 
jkrCommented:
This should work & does work, e.g.:

class C {
    public:

    int i[5][10];
};

Can you show your code?
0
 
applebyAuthor Commented:
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!
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
jkrCommented:
>>It seems like overkill to do anything more since I know
>>the size and value of the entire table

That's exactly my opinion. I'd do it the same way.
0
 
applebyAuthor Commented:
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!
0
 
applebyAuthor Commented:
Sorry about the double comment - reloaded the wrong page.  :p
0
 
applebyAuthor Commented:
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
0
 
CriusCommented:
Thanks. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now