I have a function in C that will create an array of structures:
struct Point {
int x, y;
};
void GetPoints(Point points[], int* size) {
size = 5;
points = (Points*) malloc(sizeof(Point) * size);
// ... fill in each structure with data
}
What is the correct way to call this function from C#, considering the size of points is unknown before the function is called?
There are a few mistakes in that code.
#1. If you want to modify the Point array you will need a pointer of a pointer (Or
Point** points), otherwise when you assignpoints = something, you're just changing the copy.#2. When you multiply by size, you need to dereference the pointer to, right now you're multiplying by the memory address of the array.
In the C# side, something like this should work:
References: