473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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/
Jul 22 '05 #1
5 2257
Drak I wrote:

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.
This is a FAQ.

Thanks in advance
Drak

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

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

Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5
Jul 22 '05 #2
Drak I wrote:

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.


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
kb******@gascad.at
Jul 22 '05 #3

"Drak I" <programmerr@yahoo_NOSPAM_.com> wrote in message
news:20*******************@foorum.com...

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]

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.

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.


As Karl said, read the FAQ.

john
Jul 22 '05 #4
In article <20*******************@foorum.com>,
Drak I <programmerr@yahoo_NOSPAM_.com> wrote:
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]


using vector< vector<int> > makes it all much simpler.
Jul 22 '05 #5
On 23 Jun 2004 08:18:43 GMT, Drak I <programmerr@yahoo_NOSPAM_.com>
wrote:

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


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
No**********@Home.com
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
by: Gerald | last post by:
I have a problem with an array of pointers. In a program I'm writing, I have to read a file, containing thousands of short lines. The content of another file will be compared against each line...
11
by: jacob navia | last post by:
Hi Suppose that I want to create an array of read only items I overload the operator. How can I detect if I am being called within a read context foo = Array; or within a write context...
31
by: mdh | last post by:
I am still having a problem understanding K&RII on p 112. I have looked at the FAQs --which I am sure answer it in a way that I have missed, so here goes. A 2-dim array, (per K&R) is really a...
24
by: DomoChan | last post by:
the code below will compile in visual c++ 2003, but im not sure its valid. unsigned char myString = ""; after this line executes, all the bytes within myString are indeed set to '0's' but is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.