Ask Your Question
0

What is the process for returning a double* struct in Dymola from an external C code?

asked 2022-05-16 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-12-02 20:00:00 +0000

lalupa gravatar image

To return a double* struct from an external C code in Dymola, you can follow these steps:

  1. Define the struct in Dymola using the following syntax:
type MyStruct
  Real x[2];
  Real y[2];
end MyStruct;
  1. In the external C code, create a function that returns a pointer to the struct. You can use the malloc() function to allocate memory for the struct on the heap.
MyStruct* getMyStruct() {
  MyStruct* ptr = (MyStruct*) malloc(sizeof(MyStruct));

  ptr->x[0] = 1.0;
  ptr->x[1] = 2.0;
  ptr->y[0] = 3.0;
  ptr->y[1] = 4.0;

  return ptr;
}
  1. Add an external function declaration in Dymola to call the C function and return the struct.
external "C" MyStruct* getMyStruct();
  1. Now you can use the external function in your Dymola model to get the values of the struct.
MyStruct* structPtr = getMyStruct();

Real x1 = structPtr.x[0];
Real x2 = structPtr.x[1];
Real y1 = structPtr.y[0];
Real y2 = structPtr.y[1];
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-05-16 11:00:00 +0000

Seen: 11 times

Last updated: Dec 02 '21