473,396 Members | 2,018 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,396 software developers and data experts.

multidimensional array with different row lengths

In Java, one can do the following: int[][] array = {{1,2,1}, {1},
{0,0,0,0,0,0,0,-1}};
How can one declare such array in C++/C without using various new
operators individually to each row indicies? (with a ptr-ptr)
Also, I've noticed that it works for char[][] array:
char *charArray[] = {"123456", "1515", "1"}; compiles on mingw32
but
int *intArray[] = {{1,2,1,2,1}, {11}, {0}}; wouldn't compile.

thanks =]

Jun 21 '06 #1
4 9566
ch*******@gmail.com wrote:
In Java, one can do the following: int[][] array = {{1,2,1}, {1},
{0,0,0,0,0,0,0,-1}};
How can one declare such array in C++/C without using various new
operators individually to each row indicies? (with a ptr-ptr)
Also, I've noticed that it works for char[][] array:
char *charArray[] = {"123456", "1515", "1"}; compiles on mingw32
but
int *intArray[] = {{1,2,1,2,1}, {11}, {0}}; wouldn't compile.


The following works however:

int intArray[][5] = {{1,2,1,2,1}, {11}, {0}};

Jun 21 '06 #2
ch*******@gmail.com wrote:
In Java, one can do the following: int[][] array = {{1,2,1}, {1},
{0,0,0,0,0,0,0,-1}};
How can one declare such array in C++/C without using various new
operators individually to each row indicies? (with a ptr-ptr)
Not a native array, but you can do it with a vector of vectors:

vector<vector<int> > array;

And initialisation of it is a bit problematic, you'd need to use
'copy' and 'push_back'.
Also, I've noticed that it works for char[][] array:
char *charArray[] = {"123456", "1515", "1"}; compiles on mingw32
What you have is not a "char[][] array", you have a single-dimensioned
array of pointers to char.
but
int *intArray[] = {{1,2,1,2,1}, {11}, {0}}; wouldn't compile.


Right.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 21 '06 #3
ch*******@gmail.com wrote:
In Java, one can do the following: int[][] array = {{1,2,1}, {1},
{0,0,0,0,0,0,0,-1}};
How can one declare such array in C++/C without using various new
operators individually to each row indicies? (with a ptr-ptr)
Also, I've noticed that it works for char[][] array:
char *charArray[] = {"123456", "1515", "1"}; compiles on mingw32
but
int *intArray[] = {{1,2,1,2,1}, {11}, {0}}; wouldn't compile.
...


It all depends on what it is you really need. What do you mean by "different row
lengths" and why do you need it?

If you are trying to save memory, i.e. make a sparse 2D array to consume the
amount of memory determined by the number of actually used elements, not by the
product of its maximum dimensions (m*n), then what you called 'ptr-ptr' might be
the way to go. The row arrays are not required to be allocated by 'new' though.
Your example with 'charArray' is nothing else then an implicit ptr-ptr construct
with row arrays (string literals) allocated in static memory. You can achieve
something similar with 'intArray' as well, but since there's no int-array
literals in C++, the row arrays will have to be declared explicitly:

int row1[] = { 1, 2, 1, 2, 1};
int row2[] = { 11 };
int row3[] = { 0 };

int* intArray[] = { row1, row2, row3 };

The row arrays and the 'intArray' itself can be placed in static, automatic or
dynamic ('new'-ed) memory, depending on what you need.

Of course, if you don't really care to save memory and just want to have
variable-length array initializers in your source code, you can do what Markus
suggested, i.e. use an ordinary 2D array and specify the row length explicitly

int intArray[][5] = {
{ 1, 2, 1, 2, 1 },
{ 11 },
{ 0 }
};

--
Best regards,
Andrey Tarasevich
Jun 22 '06 #4
Andrey Tarasevich wrote:
literals in C++, the row arrays will have to be declared explicitly:

int row1[] = { 1, 2, 1, 2, 1};
int row2[] = { 11 };
int row3[] = { 0 };

int* intArray[] = { row1, row2, row3 };

For this to be useful, you might need to know sizeof(row1),
sizeof(row2), ... without actually calling sizeof(row1), sizeof(...).
Is this possible ?

Jun 22 '06 #5

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

Similar topics

5
by: Rob Tweed | last post by:
Probably a simple question but I can't find the answer anyway. Specifically, is it possible to copy a multidimensional array into the $_SESSION array - ie a deep clone of all keys and data? I...
5
by: TLOlczyk | last post by:
I have a brain cramp and I need some help. I have a chunk of code below which demonstrates a problem I have with multidimensional arrays. I want to keep it simple but something specific is...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
1
by: Mark Smith | last post by:
I'm trying to copy data from a 1D array to a 2D array. The obvious thing doesn't work: int twoDee = new int; int oneDee = new int { 1, 2 }; Array.Copy(oneDee, 2, twoDee, 2, 2); This causes a...
2
by: chris | last post by:
Hi there, I created a Multidimensional array of labels Label lblMultiArray = new Label { {Label3, LblThuTotal}, {Label4,LblFriTotal} }; Now I would like to compare the values in the array,...
4
by: pauld | last post by:
$sql= sql query $i=0; while ($a2=mysql_fetch_array($a1)){array_push($temparray,$a2,$a2,$a2,$a2); { I want this array to be the value of an asssoc. array $results $results =$temparray doesnt...
1
by: David | last post by:
Hi All, Is there a way to remove an array entry completely from a multidimensional array? I have successfully "emptied" the selected array but it leaves behind an empty array entry. For...
2
by: oopsatwork | last post by:
Ok...so, I have been outside of the C world for a _very_ long time...but not so long as to remember how to do multidimensional arrays. So, let me state that I know how to malloc pointers to...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.