Solved

Best way to implement a 2d array of ints?

Posted on 2002-06-24
8
216 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
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.

705 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

19 Experts available now in Live!

Get 1:1 Help Now