Arrays in Structures

I am new to C and I have problems with array types when embedding in structures. Below is an example of my problem:

typedef struct {
    double J[151][151];
} *UserData;

static int PSolve(void *user_data, N_Vector solution)
{
UserData data;
data = (UserData) user_data;

double J[151][151];
J = data->J;

/* Solve a matrix equation that uses J, stored in 'solution' */

return(0);
}

When I try to compile this, I get an error: incompatible types when assigned to type 'double [151] [151] from type' double (*) [151]

My current workaround for this is to replace “J [x] [y]” with “data-> J [x] [y]” in the code to solve the matrix equation, but profiling has shown that this is less efficient.,

Changing the arguments to PSolve is not an option, because I use the sundials-cvode solver, which prescribes the type and order of the arguments.

+1
source share
4 answers
typedef struct {
    double J[151][151];
} UserData; // This is a new data structure and should not a pointer!

static int PSolve(void *user_data, N_Vector solution)
{
UserData* data; // This should be a pointer instead!
data = (UserData*) user_data;

double J[151][151];
memcpy(J, data->J, sizeof(double) * 151 * 151); // use memcpy() to copy the contents from one array to another

/* Solve a matrix equation that uses J, stored in 'solution' */

return(0);
}
+5
source

, C. memcpy(), karlphillip.

, , , . , solution , user_data/data solution. , , C99 restrict, :

static int PSolve(void * restrict user_data, N_Vector restrict solution)

promises , , data->J, .

restrict C89 , __restrict, .

+2

. C.

, - :

double (*J)[151] = data->J;

151. typedefs

typedef double line[151];
line *J = data->J;

, , .

: , , , , . , , " ", . .

, , ( -S), , - .

0

Try defining the local variable double J[151][151]as double **J.

-1
source

All Articles