B
Brian K. Michalk
I know I can assign default values to a structure during
initialization, but I have a routine that accumulates a lot of floats
into an array, and then I would like to assign them to a (reference to
a) structure before exiting. I would like something analogous to
strcpy, but for structures. My example:
in file foo.h:
-----------------------------------
struct _stats {
float slope;
float mpd;
float macro_sum;
float grabf[13][6];
};
typedef struct _stats t_stats;
t_stats ts_min;
in file foo.c:
-----------------------------------
#include "foo.h"
void some_routine_to_reset_ts_min(void) {
int i;
float *p_to_ts_min;
float some_array[81]= {0.01, 0.02, 0.03 ... 0.81};
// now populate the global ts_min structure
// it would be great to have the equivalent of strcpy, like
structcpy(ts_min, some_array);
// assigning by element name is tedious and prohibits easy code
change of
// constants determined from lab results like:
ts_min.slope = 0.01; // doable but difficult to revize
constants
// is it valid to do the following?
p_to_ts_min = &ts_min;
for (i=0; i<81; i++) {
p_to_ts_min = some_array;
}
}
Is there a better way to do this?
initialization, but I have a routine that accumulates a lot of floats
into an array, and then I would like to assign them to a (reference to
a) structure before exiting. I would like something analogous to
strcpy, but for structures. My example:
in file foo.h:
-----------------------------------
struct _stats {
float slope;
float mpd;
float macro_sum;
float grabf[13][6];
};
typedef struct _stats t_stats;
t_stats ts_min;
in file foo.c:
-----------------------------------
#include "foo.h"
void some_routine_to_reset_ts_min(void) {
int i;
float *p_to_ts_min;
float some_array[81]= {0.01, 0.02, 0.03 ... 0.81};
// now populate the global ts_min structure
// it would be great to have the equivalent of strcpy, like
structcpy(ts_min, some_array);
// assigning by element name is tedious and prohibits easy code
change of
// constants determined from lab results like:
ts_min.slope = 0.01; // doable but difficult to revize
constants
// is it valid to do the following?
p_to_ts_min = &ts_min;
for (i=0; i<81; i++) {
p_to_ts_min = some_array;
}
}
Is there a better way to do this?