c - "initializer element is not constant" and "near initialization" -


i want return array, , since doesn't matter if gets overwritten, method this:

double * kryds(double linje_1[], double linje_2[]){ double x = linje_1[1]*linje_2[2]-linje_1[2]*linje_2[1]; double y = linje_1[2]*linje_2[0]-linje_1[0]*linje_2[2]; double z = linje_1[0]*linje_2[1]-linje_1[1]*linje_2[0]; static double svar[] = {x, y, z}; return svar; } 

however, error @ static double svar[] = {x, y, z}; . can please explain means? i've read other forum posts this one doesn't apply case

first of all, means says

  • initializers must constant expressions,

but not can't return local array because it's undefined behavior (making static solves problem causes other problems), instead try struct.

struct point3d {double x; double y; double z; }; struct point3d kryds(double *linje_1, double *linje_2) {     struct point3d result;     result.x = linje1[1] * linje2[2] - ...     result.y = ...      // , on     return result; } 

or if must return double *, try malloc() better static double svar[]

double *svar;  svar = malloc(3 * sizeof(*svar)); if (svar == null) // never happen, must check     return null; svar[0] = linje_1[1] * linje_2[2] - linje_1[2] * linje_2[1]; // , on 

you need remember free() pointer later on when know sure wont use anymore.

or, if insist

static double svar[3];  svar[0] = linje_1[1] * linje_2[2] - linje_1[2] * linje_2[1]; // , on 

and don't need auxiliary x, y , z in of proposed solutions in answer.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -