You are returning a pointer to a stack memory location. That memory region is no longer valid once the function returns.
Also, instead of actually returning a pointer to the array, you are returning the first element in the array. The following code will return 1, not a pointer the array.
int array[] {1, 2, 3, 4};
return *array
You probably only need to make minimal changes your code to get it to work.
void** array = (void **) malloc(sizeof(void *) * array_size);
...
return array;
Just make sure that you release the memory that memory used for array when you are finished with it.
void **array = list_to_array(list);
// Use array
...
// Finished with array
free(array);
Answer from torak on Stack OverflowYou are returning a pointer to a stack memory location. That memory region is no longer valid once the function returns.
Also, instead of actually returning a pointer to the array, you are returning the first element in the array. The following code will return 1, not a pointer the array.
int array[] {1, 2, 3, 4};
return *array
You probably only need to make minimal changes your code to get it to work.
void** array = (void **) malloc(sizeof(void *) * array_size);
...
return array;
Just make sure that you release the memory that memory used for array when you are finished with it.
void **array = list_to_array(list);
// Use array
...
// Finished with array
free(array);
When you increase the pointer int* a by 1, it would actually increase it by sizeof(int), which is - on most systems, at least - 4.
So if
int* a = 0x40b8c438
then
a + 1
= ((void*) a) + sizeof(int)
= 0x40b8c43c
and
a + 2
= ((void*) a) + sizeof(int) * 2
= 0x40b8c440
Need help with converting an array to a list in C#` - Questions & Answers - Unity Discussions
How do I convert a Python list into a C array by using ctypes? - Stack Overflow
c# - Converting a list to an array with ToArray() - Stack Overflow
Converting a list to an array - Mathematica Stack Exchange
Save yourself some pain...
using System.Linq;
int[] ints = new [] { 10, 20, 10, 34, 113 };
List<int> lst = ints.OfType<int>().ToList(); // this isn't going to be fast.
Can also just...
List<int> lst = new List<int> { 10, 20, 10, 34, 113 };
or...
List<int> lst = new List<int>();
lst.Add(10);
lst.Add(20);
lst.Add(10);
lst.Add(34);
lst.Add(113);
or...
List<int> lst = new List<int>(new int[] { 10, 20, 10, 34, 113 });
or...
var lst = new List<int>();
lst.AddRange(new int[] { 10, 20, 10, 34, 113 });
There is also a constructor overload for List that will work... But I guess this would required a strong typed array.
//public List(IEnumerable<T> collection)
var intArray = new[] { 1, 2, 3, 4, 5 };
var list = new List<int>(intArray);
... for Array class
var intArray = Array.CreateInstance(typeof(int), 5);
for (int i = 0; i < 5; i++)
intArray.SetValue(i, i);
var list = new List<int>((int[])intArray);
The following code works on arbitrary lists:
import ctypes
py_values = [1, 2, 3, 4]
arr = (ctypes.c_int * len(py_values))(*py_values)
This is an explanation of the accepted answer.
ctypes.c_int * len(pyarr) creates an array (sequence) of type c_int of length 4 (python3, python 2). Since c_int is an object whose constructor takes one argument, (ctypes.c_int * len(pyarr)(*pyarr) does a one shot init of each c_int instance from pyarr. An easier to read form is:
pyarr = [1, 2, 3, 4]
seq = ctypes.c_int * len(pyarr)
arr = seq(*pyarr)
Use type function to see the difference between seq and arr.
This is the error (as pointed out from Darkshadw and Jon Skeet)
listItem myList = new List<listItem>();
You are assigning the value of a List to a listItem.
Replace it with
List<listItem> myList = new List<listItem>();
to create a list of listItem. Then
listItem[] myArray = myList.ToArray();
will work.
have you tried
listItem[] myArray = myList.ToArray(new listItem[]{});
in Java it works, im not sure in c#
What you're calling my_array may be thought of as a function, which you can assign like:
myfun[i_, j_] := list[[i, j]];
or, to shift the indices:
myfun[i_, j_] := list[[i+1, j+1]];
SeedRandom[1]
list = RandomInteger[10, {4, 5}]
{{1, 4, 0, 7, 0}, {0, 8, 6, 0, 4}, {1, 8, 5, 1, 1}, {1, 3, 2, 10, 1}}
ClearAll[myArray]
myArray = Indexed[list, 1 + {##}] /. Indexed[_, ___] :> Undefined &;
{myArray[0, 1], myArray[3, 2], myArray[4, 6]}
{4, 2, Undefined}