views:

34

answers:

3

how can I convert array<int^>^ to int*?

A: 

Name of the array is the address of first element in the array.

int array[] = {1, 2, 3, 4, 5};
int* p = array;
Hemant
Yes, we know. But this doesn't answer the question.
klez
assume I have:array<int^> ^p;and I want to pass p as an argument to f(int* input);what should I do?
HPT
A: 

I think it will be hard to directly convert array<int^>^ into an int*, because it is an array of references to ints, not an array of ints. There is no promises about the memory layout of the integers themselves, which is required in order to get them to a plain old C/C++ array.

I think that the easiest way to go is to make a copy of the array, pass it to f(int* input) and then possibly copy the data back if it is modified by f.

Anders Abel
+1  A: 

You cannot, at least not the simple way.

If you mean array<int>^ to int*, you can do following:

array<int>^ arr;
cli::pin_ptr<int> pArrayElement = &arr[0];

and then use classical pointer arithmetic over the pin_ptr.

Yossarian