Passing a mixed-type parameters to a function as a pointer, I think

this my first time posting here. Please forgive my inexperience; I'm just learning.

I'm trying to implement a GSL root-finding method, and it requires passing the dependent variable and a struct of parameters to the function.

Here's the parameter struct that gets passed to the function (it's in a .h file):

struct SumForces_params
double curvature;
struct panel panels[1000];
int lenPanels;

Here's where I define an instance of the parameters:

struct SumForces_params params = {0.0000001, {panels[1000]}, lenPanels};

Here's the function:

double SumForces(double NAloc, void *params)
struct SumForces_params *p = (struct SumForces_params *) params;
double curvature = p->curvature; //works fine
int lenPanels = p->lenPanels; //works fine

struct panel panels = p->panels[1000];
//~ printf("panel 421 location = %g\n", panels[421].Yloc); 
//~ Above line gives this error: SumForces.c:54: error: subscripted value is neither
//~ array nor pointer

double yloc = 0;
yloc = p->panels[421].Yloc;
printf("panel 421 location = %g\n", yloc);

In main, this gives me the expected output:

printf("Main Panel 421 loc = %g\n",panels[421].Yloc);

But as noted from the function code, struct panel panels = p->panels[1000]; doesn't work, and the output from the second attempt (the last three lines) returns zero. Anyone see what I'm doing wrong?

Here's my definition of struct panel:

struct panel
   double strain[136];
   double stress[136];
   double AE[136];
   double Ys;
   double E;
   double Yloc;
   double Area;
   gsl_interp_accel *acc_stress;
   gsl_spline *spline_stress;
   gsl_interp_accel *acc_AE;
   gsl_spline *spline_AE;

And here is where I define panels as an array of 1000 panel structures:

struct panel panels[1000];


struct panel panels = p->panels[1000];

p->panel is an array of 1000 elements. The last element has index 999 since you start indexing at 0. So, this particular line invokes UB. You probably want:

struct panel panels = p->panels[ 999 ];

The compiler probably understood your syntax in a wrong way (not what you expected), and didn't warn about this:

struct SumForces_params params = {0.0000001, {panels[1000]}, lenPanels};

I guess from your question that you want to copy the data about panels into params. The correct way to do it is like this:

    struct SumForces_params params =
        {}, // C doesn't have syntax for "put panels here"
    memcpy(params.panels, panels, sizeof(params.panels));

Need Your Help

Nearly always write classes with no state as singletons?

java singleton

Today I saw the following JavaFX related code in a project:

Optimal way to generate list of PHP object properties with delimiter character, implode()?

php list object iteration implode

I am trying to find out if there is a more optimal way for creating a list of an object's sub object's properties. (Apologies for the crude wording, I am not really much of an OO expert)