T
tobiasoed
Hello,
I want to do something similar to this:
#include <stdlib.h>
struct coord {
double val;
double err;
};
struct point{
double value;
char foo;
struct coord coords[];
};
struct expe{
struct point *pts;
int np;
int nc;
};
struct expe *create_expe(int nc, int np){
struct expe *expe_p;
/* check for NULL omitted for simplicity */
expe_p = malloc(sizeof *expe_p);
expe_p->pts = malloc(np * (sizeof *expe_p->pts
+ nc * sizeof *expe_p->pts->coords));
expe_p->np = np;
expe_p->nc = nc;
return expe_p;
}
struct point *point(struct expe *expe_p, int i) {
return (struct point *) (
(char *) expe_p->pts
+ i * (sizeof *expe_p->pts
+ expe_p->nc * sizeof *expe_p->pts->coords)
);
}
int main(void) {
struct expe *expe_p=create_expe(4,100);
point(expe_p,2)->value=3;
point(expe_p,2)->coords[1].val=41;
point(expe_p,2)->coords[1].err=34;
return 0;
}
It compiles wo. warning with gcc 4.1.0 and runs ok but I'm not
sure if the the char foo in points can mess up the alignement
requirements of elements of the pts array. Any language lawyer
out there?
Tobias.
I want to do something similar to this:
#include <stdlib.h>
struct coord {
double val;
double err;
};
struct point{
double value;
char foo;
struct coord coords[];
};
struct expe{
struct point *pts;
int np;
int nc;
};
struct expe *create_expe(int nc, int np){
struct expe *expe_p;
/* check for NULL omitted for simplicity */
expe_p = malloc(sizeof *expe_p);
expe_p->pts = malloc(np * (sizeof *expe_p->pts
+ nc * sizeof *expe_p->pts->coords));
expe_p->np = np;
expe_p->nc = nc;
return expe_p;
}
struct point *point(struct expe *expe_p, int i) {
return (struct point *) (
(char *) expe_p->pts
+ i * (sizeof *expe_p->pts
+ expe_p->nc * sizeof *expe_p->pts->coords)
);
}
int main(void) {
struct expe *expe_p=create_expe(4,100);
point(expe_p,2)->value=3;
point(expe_p,2)->coords[1].val=41;
point(expe_p,2)->coords[1].err=34;
return 0;
}
It compiles wo. warning with gcc 4.1.0 and runs ok but I'm not
sure if the the char foo in points can mess up the alignement
requirements of elements of the pts array. Any language lawyer
out there?
Tobias.