Solved

Best way to implement a 2d array of ints?

Posted on 2002-06-24
8
228 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
 
LVL 86

Accepted Solution

by:
jkr earned 100 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ - Convert a wString to char * 9 513
c++ mixing int and strings in multi array 8 71
how to understand recursion 12 218
How to programmatically differentiate between C and Java 10 188
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

910 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now