Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Best way to implement a 2d array of ints?

Posted on 2002-06-24
8
Medium Priority
?
275 Views
Last Modified: 2010-04-01
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
Comment
Question by:appleby
  • 4
  • 2
  • 2
8 Comments
 
LVL 3

Expert Comment

by:Crius
ID: 7105059
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
 
LVL 86

Expert Comment

by:jkr
ID: 7105066
This should work & does work, e.g.:

class C {
    public:

    int i[5][10];
};

Can you show your code?
0
 

Author Comment

by:appleby
ID: 7105101
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
ID: 7105112
>>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
 

Author Comment

by:appleby
ID: 7105113
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
 

Author Comment

by:appleby
ID: 7105119
Sorry about the double comment - reloaded the wrong page.  :p
0
 

Author Comment

by:appleby
ID: 7105167
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
 
LVL 3

Expert Comment

by:Crius
ID: 7105186
Thanks. :)
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question