Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > two dimensional array

Reply
Thread Tools

two dimensional array

 
 
Someonekicked
Guest
Posts: n/a
 
      04-10-2005
im trying to declare a two dimensional array using pointers. but it is not
working, any ideas?


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-10-2005
* Someonekicked:
> im trying to declare a two dimensional array using pointers. but it is not
> working, any ideas?


Use std::vector.

For concrete examples, see chapter 1.7 of
<url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01.html>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      04-10-2005
"Someonekicked" writes:

> im trying to declare a two dimensional array using pointers. but it is not
> working, any ideas?


Like this:

#include <iostream>
using namespace std;

//------------------
void test2(int b[][5])
{
cout << b[2][3] << endl;
}
//====================
int main()
{
int(*a)[5] = new int[3][5];
a[2][3] = 2048;
test2(a);
cin.get();
}


 
Reply With Quote
 
Val
Guest
Posts: n/a
 
      04-10-2005

"Someonekicked" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
| im trying to declare a two dimensional array using pointers. but it is not
| working, any ideas?
|

Programming is communicating in the first place.
1) Does the matrix know the rows and columns at compile time or run time only?
2) Why isn't working? Where is the code?
3) How did you express the Matrix? As a single array of row*col size, or as an array holding arrays?

See the C++ faq lite (google it) for your first hints.


 
Reply With Quote
 
Axter
Guest
Posts: n/a
 
      04-11-2005

Someonekicked wrote:
> im trying to declare a two dimensional array using pointers. but it

is not
> working, any ideas?


There are many ways to create a two dimensional array in C++.

You can use a vector of vectors
std::vector<std::vector<int> > My2dOfInt;

You can also use a custom class, or you can use a pointers of pointers.

Check out the following link for examples, and for different methods.
http://www.tek-tips.com/faqs.cfm?fid=5575

 
Reply With Quote
 
Angad
Guest
Posts: n/a
 
      04-11-2005
I would say, the simplest way would be

int **p ;
p = new int *[ROWS] ;
for( int i = 0 ; i < ROWS ; i++ )
p[i] = new int[COLUMNS];

But everyone seems to suggest vectors...

 
Reply With Quote
 
Axter
Guest
Posts: n/a
 
      04-11-2005

Angad wrote:
> I would say, the simplest way would be
>
> int **p ;
> p = new int *[ROWS] ;
> for( int i = 0 ; i < ROWS ; i++ )
> p[i] = new int[COLUMNS];
>
> But everyone seems to suggest vectors...



You're missing the code to free the above allocated data.
Using **p (pointer of pointers) you need code for allocating the data,
code for keeping track of the sizes of the rows and columns and code to
deallocated the data.

So using **p is not the simplest approach.
It's far easier to use a vector that wraps all the logic behind a safe
container.
Your above code can be done in one line of code by using vectors.
vector<vector<int> > My2DArr(ROWS, std::vector<int>(COLUMNS));

IMHO, this one line of code is far simpler then the pointer of pointer
code, and the required tracking code, plus the required deallocation
code needed for the pointer of pointer method.

 
Reply With Quote
 
Val
Guest
Posts: n/a
 
      04-11-2005
| Your above code can be done in one line of code by using vectors.
| vector<vector<int> > My2DArr(ROWS, std::vector<int>(COLUMNS));

std::valarray for numbers.

Or single array acting as a Matrix:
int* Matrix = new int[ROWS*COLS];

But I think it's a nice excercise to do it the ** way


 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      04-11-2005
Someonekicked wrote:

> I'm trying to declare a two dimensional array using pointers.


Take a look at the FAQ

http://www.parashift.com/c++-faq-lite/

[16.16] How do I allocate multidimensional arrays using new?
 
Reply With Quote
 
Angad
Guest
Posts: n/a
 
      04-12-2005
Yes, I agree that vectors simplify the matter a lot by reducing our
coding, but I must say that they add to a lot of unnecessary code to
the executable code. Compile time increases manyfold (I'm talking from
a personal experience). I'd say, use vectors only when basic data types
cannot be used....

As for the freeing of the memory, the code is:

for( int i = 0 ; i < ROWS ; i++ )
delete [] p[i] ;
delete [] p ;

This code will definitely be faster than using vectors... (please
comment if I'm wrong)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To convert a one dimensional array into a two dimensional array using C amrutha0303 Software 0 08-03-2010 10:02 PM
How do copy Strings from a single dimensional array to double dimensional array Venkat C++ 4 12-05-2003 09:23 AM
Re: Two dimensional pointers and Two dimensional arrays Icosahedron C++ 8 08-21-2003 05:15 AM
Re: Two dimensional pointers and Two dimensional arrays John Harrison C++ 4 08-19-2003 04:00 PM
Re: Two dimensional pointers and Two dimensional arrays Alf P. Steinbach C++ 0 08-18-2003 08:25 AM



Advertisments