Connecting Tech Pros Worldwide Help | Site Map

Array intialization

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 02:06 PM
Drak I
Guest
 
Posts: n/a
Default Array intialization


Hello all,
I have very basic question on intializing two dimenstional array. Why cant I
initialize a two dimensional int array like the following ?

int **intArray = new int[10][20]

What is the best way to intialize such array if I need to pass the size as
variables, like int [rows][columns], where rows and columns has the size of the
array.

Thanks in advance
Drak

--
Use our news server 'news.foorum.com' from anywhere.
More details at: http://nnrpinfo.go.foorum.com/

  #2  
Old July 22nd, 2005, 02:45 PM
Karl Heinz Buchegger
Guest
 
Posts: n/a
Default Re: Array intialization

Drak I wrote:[color=blue]
>
> Hello all,
> I have very basic question on intializing two dimenstional array. Why cant I
> initialize a two dimensional int array like the following ?
>
> int **intArray = new int[10][20]
>
> What is the best way to intialize such array if I need to pass the size as
> variables, like int [rows][columns], where rows and columns has the size of the
> array.[/color]

This is a FAQ.
[color=blue]
>
> Thanks in advance
> Drak
>
> --
> Use our news server 'news.foorum.com' from anywhere.
> More details at: http://nnrpinfo.go.foorum.com/[/color]


--
Karl Heinz Buchegger, GASCAD GmbH
Teichstrasse 2
A-4595 Waldneukirchen
Tel ++43/7258/7545-0 Fax ++43/7258/7545-99
email: kbuchegg@gascad.at Web: www.gascad.com

Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5
  #3  
Old July 22nd, 2005, 02:45 PM
Karl Heinz Buchegger
Guest
 
Posts: n/a
Default Re: Array intialization

Drak I wrote:[color=blue]
>
> Hello all,
> I have very basic question on intializing two dimenstional array. Why cant I
> initialize a two dimensional int array like the following ?
>
> int **intArray = new int[10][20]
>
> What is the best way to intialize such array if I need to pass the size as
> variables, like int [rows][columns], where rows and columns has the size of the
> array.
>[/color]

This is a FAQ

http://www.parashift.com/c++-faq-lit...html#faq-16.15

Make sure you read the following items also.


--
Karl Heinz Buchegger
kbuchegg@gascad.at
  #4  
Old July 22nd, 2005, 03:15 PM
John Harrison
Guest
 
Posts: n/a
Default Re: Array intialization


"Drak I" <programmerr@yahoo_NOSPAM_.com> wrote in message
news:2004623-101843-993322@foorum.com...[color=blue]
>
> Hello all,
> I have very basic question on intializing two dimenstional array. Why cant[/color]
I[color=blue]
> initialize a two dimensional int array like the following ?
>
> int **intArray = new int[10][20]
>[/color]

Because the types are different. intArray is type int** but new int[10][20]
is type int (*)[20]. Not the same type at all. Newbies often get this wrong
because they don't understand the C++ type system very well.

[color=blue]
> What is the best way to intialize such array if I need to pass the size as
> variables, like int [rows][columns], where rows and columns has the size[/color]
of the[color=blue]
> array.[/color]

As Karl said, read the FAQ.

john


  #5  
Old July 22nd, 2005, 03:15 PM
Mats Weber
Guest
 
Posts: n/a
Default Re: Array intialization

In article <2004623-101843-993322@foorum.com>,
Drak I <programmerr@yahoo_NOSPAM_.com> wrote:
[color=blue]
>I have very basic question on intializing two dimenstional array. Why cant I
>initialize a two dimensional int array like the following ?
>
>int **intArray = new int[10][20][/color]

using vector< vector<int> > makes it all much simpler.
  #6  
Old July 22nd, 2005, 03:15 PM
Bob Hairgrove
Guest
 
Posts: n/a
Default Re: Array intialization

On 23 Jun 2004 08:18:43 GMT, Drak I <programmerr@yahoo_NOSPAM_.com>
wrote:
[color=blue]
>
>Hello all,
>I have very basic question on intializing two dimenstional array. Why cant I
>initialize a two dimensional int array like the following ?
>
>int **intArray = new int[10][20]
>
>What is the best way to intialize such array if I need to pass the size as
>variables, like int [rows][columns], where rows and columns has the size of the
>array.
>
>Thanks in advance
>Drak[/color]

Scott Meyers has a nice chapter on implementing a 2-dimensional array
class in "More Effective C++", item 30: "Proxy classes".

However, you can also use a vector of vectors instead of unsafe
arrays. Note that vectors -- unlike arrays -- can also be dynamically
sized, so this is what you need if your dimensions aren't known at
compile time.

// e.g.:

#include <iostream>
#include <ostream>
#include <vector>

typedef std::vector<int> IntVec;
typedef std::vector<IntVec> IntArray2D;

const int IDX_1 = 10;
const int IDX_2 = 20;

int main() {
using std::cout;
using std::endl;

IntVec dummy(IDX_2); // reserving space in the ctor
IntArray2D x(IDX_1); // avoids unnecessary reallocations

int c=0;
for (int i=0; i<IDX_1; ++i) {
for (int j=0; j<IDX_2; ++j, c++)
dummy[j] = (c);
x[i] = dummy;
}
for (int i=0; i<IDX_1; ++i) {
cout << "x[" << i << "]:";
for (int j=0; j<IDX_2; ++j)
cout << " " << x[i][j];
cout << endl;
}
return 0;
}

I'm not sure how to reserve enough space up front for IntArray2D,
though; assigning directly to x[i][j] instead of filling up the dummy
will compile, but then gives me an access violation at run-time. The
use of a temporary vector<int> (i.e. "dummy") seems necessary in order
for it to work.

Once the 2D-vector has been initialized, it should not be a problem to
assign directly using just the index values.

--
Bob Hairgrove
NoSpamPlease@Home.com
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.