473,396 Members | 2,082 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.

int[,] array in C++?

Hello,
I am failing to create appropriate array in C++ to pass to a C# method.
The method looks like
public Foo(int[,] data) {..}

in C#. When I try to call it with an array initialized in C++ as:

int inputArray[][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};

it fails. So what is the proper way to declare an equivalent of int[,]
in C++?

Nov 28 '06 #1
8 4053
<ol***************@googlemail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
I am failing to create appropriate array in C++ to pass to a C# method.
The method looks like
public Foo(int[,] data) {..}

in C#. When I try to call it with an array initialized in C++ as:

int inputArray[][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};

it fails. So what is the proper way to declare an equivalent of int[,]
in C++?
Well, the purists will wince when they read this but ...

.... which C++ language are you talking about? :-)

Arrays in C# are instances of the System::Array class.

In MC++ (VS 2003), to create a managed array of integers you'd make the
declaration

Int32 inputArray[,] = new Int32[4,2];

and the compiler will do the right thing.

Alternatively, you should be able to cook up something using

Array::CreateInstance()

I've not used C++/CLI (in VS 2005) much so I'm not all that familiar with
the syntax using it. My guess is that new is replaced by gcnew in the line
above but that is a WAG.

As for ISO C++ - if you really want your C# code to see a managed array - I
don't think it is an option though I could be wrong.

Regards,
Will

Nov 28 '06 #2
In C++/CLI, you'd declare the array as:
array<int, 2^foo;
or (with value initialization):
array<int, 2^foo = {{1,2}, {3,4}};
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
"ol***************@googlemail.com" wrote:
Hello,
I am failing to create appropriate array in C++ to pass to a C# method.
The method looks like
public Foo(int[,] data) {..}

in C#. When I try to call it with an array initialized in C++ as:

int inputArray[][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};

it fails. So what is the proper way to declare an equivalent of int[,]
in C++?

Nov 28 '06 #3
I've not used C++/CLI (in VS 2005) much so I'm not all that familiar with
the syntax using it. My guess is that new is replaced by gcnew in the line
above but that is a WAG.
array<int, 2>^ arr = gcnew array<int, 2>(3, 4);

Marcus
Nov 28 '06 #4
Thank Will and David,
array<int, 2^foo = {{1,2}, {3,4}}; is what I needed!

Nov 28 '06 #5
Thanks Will and David,
array<int, 2^foo = {{1,2}, {3,4}}; is what I needed!

Nov 28 '06 #6
<ol***************@googlemail.comwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...
Thank Will and David,
array<int, 2^foo = {{1,2}, {3,4}}; is what I needed!
It's a good thing that thanks aren't necessary because Marcus (who deserved
one) didn't get one and I (who didn't deserve one because I offered a VS2003
solution) got one. :-)

Seriously, the thing that you should take away from this exchange is that
unlike the native case where arrays are simply "objects" layed out
sequentially in memory where the "next" storage location may or may not be
what you expect, arrays are types in their own right in .Net in which the
array's "size" is known.

Regards,
Will
Nov 28 '06 #7
By the way, I gave you the short-form value initialization, which is only
valid in a declaration.
Some people may prefer the long form, which is:
array<int, 2^foo = gcnew array<int, 2>(2,2) {{1,2}, {3,4}};
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
"ol***************@googlemail.com" wrote:
Thank Will and David,
array<int, 2^foo = {{1,2}, {3,4}}; is what I needed!

Nov 28 '06 #8
William DePalo [MVP VC++] schrieb:
<ol***************@googlemail.comwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...
Thank Will and David,
array<int, 2^foo = {{1,2}, {3,4}}; is what I needed!

It's a good thing that thanks aren't necessary because Marcus (who deserved
one) didn't get one and I (who didn't deserve one because I offered a VS2003
solution) got one. :-)

Seriously, the thing that you should take away from this exchange is that
unlike the native case where arrays are simply "objects" layed out
sequentially in memory where the "next" storage location may or may not be
what you expect, arrays are types in their own right in .Net in which the
array's "size" is known.

Regards,
Will
Typing takes some time, I just overlooked Markus's answer :) Thanks to
you too, Markus!

Nov 28 '06 #9

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

Similar topics

4
by: J. Campbell | last post by:
I'm a novice with c/c++ and have been reading Eckel's book. I'd like some feedback on using this method. What I need to do is treat a string as numeric data. I know how to write functions to...
5
by: Kemal Ozan | last post by:
Hi, I am studying K&R book. On the multidimensional Arrays chapter they say "int (*daytab) is a pointer to an array of 13 integers. The parenthesis are necessary since brackets have higher...
4
by: Anupam | last post by:
Hi, This was asked on our school newsgroup by a friend of mine. He gave the following code and said that it did not print out the elements of the array. He asked for an explanation for this...
8
by: Devrobcom | last post by:
Hi I have an array with 30 int and another array with 1000 int. If any of the 30 intergers can be found in the 1000 int array I shall return true. The contents of the arrays will also change over...
4
by: Bilgehan.Balban | last post by:
Hi, The following code: #include <stdio.h> // const int const_asize = 10; #define define_asize = 10; int array = {1,2,3,4,5,6,7,8,9,0};
3
by: Albert Albani | last post by:
Hello I have a problem that I cannot seem to solve I have a c funtion in a DLL that basically looks like func_c (some_struct* s) {...} some_struct is defined like so: struct some_struct {
5
by: Galahad | last post by:
I am using the code below to create a integer array. while(dr.Read()) { string StateID=dr.ToString(); int FacilityID=Convert.ToInt32(dr.ToString()); int FacilityIDs={FacilityID}; } ...
12
by: andrew_nuss | last post by:
Hi, I have a template array class on T with specialization for bloat when T is void*. Thus when T is really an int*, my base ptr is physically a void** and needs to be converted to an int**. ...
8
by: beagle197 | last post by:
Folks, Attempting to q-sort an array of int pairs, e.g. {{0,1}, {0, 0}, ...} allocated using malloc/calloc, but the arguments I'm passing to qsort are producing the incorrect results (see...
10
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, There is error message when executing my program, Unhandled exception at 0x00411a49 in test_entern.exe: 0xC0000005: Access violation reading location 0x00000002. It is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
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...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.