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 =] 4 9600 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}}; 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<i nt> > 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 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
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 ? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 naively assumed that
$_SESSION = $myArray ;
would work but it doesn't appear to work. Is there a single function
|
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 getting in the way.
int a;
int b;
int **present;
int **next;
|
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 function (which was translated from Fortran code),
among other heinous and numerous declarations, is this bit:
static float bbuff;
static int bkey;
static int buse;
|
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 RankException. But the MSDN documentation says:
When copying between multidimensional arrays, the array
|
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, comparing the text in
Label3 and LblThuTotal and the text in Label4 and LblFriTotal.
| |
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 work
$results =$temparray doesnt work
$results =$temparray doesnt work
|
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 example:
mainArray = ,,,,]
Example of the nested arrays:
|
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
pointers and then malloc my data space. This question is NOT about
that.
I need to keep a dynamically sized (a result of command line options)
matrix in a shared memory segment created via shmget and attached via
shmat. My first instinct was to do...
|
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 function:
x = new int;
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |