当前位置: 动力学知识库 > 问答 > 编程问答 >

pointers - C element of struct inside strunct not retaining assigned value

问题描述:

Here's my structs :

typedef struct Bounds Bounds;

struct Bounds

{

int type;

double lb;

double ub;

};

typedef struct HelperGlpk HelperGlpk;

struct HelperGlpk

{

double *matrix_coefs;

double *obj_coefs;

Bounds *row_bounds;

Bounds *col_bounds;

int *column_of_coef;

int *row_of_coef;

int cpt_coef;

int cpt_contrainte;

};

here's my function generate_contrainte_1 :

void genere_contrainte_1(int i, int j, HelperGlpk *helper_glpk, Baie baie){

helper_glpk->col_bounds[index_ouverture_serveur(i)].type = GLP_DB;

helper_glpk->col_bounds[index_ouverture_serveur(i)].lb = 0;

helper_glpk->col_bounds[index_ouverture_serveur(i)].ub = 1;

helper_glpk->col_bounds[index_connexion(i, j, baie.nbr_serveur)].type = GLP_LO;

helper_glpk->col_bounds[index_connexion(i, j, baie.nbr_serveur)].lb = 0;

helper_glpk->col_bounds[index_connexion(i, j, baie.nbr_serveur)].ub = 0;

}

and here's how I call generate_contrainte_1 :

HelperGlpk helper_glpk;

helper_glpk.col_bounds = malloc((nbr_colums + 1) * sizeof(Bounds));

helper_glpk.row_bounds = malloc((nbr_rows + 1) * sizeof(Bounds));

for(i = 1; i <= baie.nbr_serveur; i++)

for(j = 1; j <= baie.nbr_client; j++)

genere_contrainte_1(i, j, &helper_glpk, baie);

The problem is that the value inside

helper_glpk->col_bounds[index_ouverture_serveur(i)].type

is not saved outside my function and I get a wrong value when I try to access it.

How can I solve this problem ?

Edit :

int index_ouverture_serveur(int i){

return i;

}

int index_connexion(int i, int j, size_t nbr_serveur){

return j * nbr_serveur + i;

}

Edit 2 :

I added some printf to know where the error come from, it didn't help me but it might help you :

 helper_glpk->col_bounds[index_ouverture_serveur(i)].type = GLP_DB;

helper_glpk->col_bounds[index_ouverture_serveur(i)].lb = 0;

helper_glpk->col_bounds[index_ouverture_serveur(i)].ub = 1;

printf("col_bounds[%d].type = %d\n", index_ouverture_serveur(i), helper_glpk->col_bounds[index_ouverture_serveur(i)].type);

helper_glpk->col_bounds[index_connexion(i, j, baie.nbr_serveur)].type = GLP_LO;

helper_glpk->col_bounds[index_connexion(i, j, baie.nbr_serveur)].lb = 0;

helper_glpk->col_bounds[index_connexion(i, j, baie.nbr_serveur)].ub = 0;

printf("col_bounds[%d].type = %d\n", index_connexion(i, j, baie.nbr_serveur), helper_glpk->col_bounds[index_ouverture_serveur(i)].type);

here's what I get :

col_bounds[1].type = 4

col_bounds[51].type = 4

col_bounds[1].type = 4

col_bounds[101].type = 4

col_bounds[1].type = 4

col_bounds[151].type = 4

col_bounds[1].type = 4

[...]

col_bounds[4900].type = 4

col_bounds[50].type = 4

col_bounds[4950].type = 4

col_bounds[50].type = 4

col_bounds[5000].type = 4

col_bounds[50].type = 4

col_bounds[5050].type = 4

now I do this printf outside my function :

for(i = 1; i <= nbr_colums; i++)

printf("type[%d] : %d\n", i, helper_glpk.col_bounds[i].type);

and this is what I get :

type[1] : 2555

type[2] : 2558

type[3] : 2561

type[4] : 2564

type[5] : 2567

type[6] : 2570

type[7] : 2573

type[8] : 2576

type[9] : 2579

type[10] : 2582

type[11] : 2585

type[12] : 2588

type[13] : 2591

type[14] : 2594

type[15] : 2597

type[16] : 2600

type[17] : 2603

type[18] : 2606

type[19] : 2609

type[20] : 2612

type[21] : 2615

type[22] : 2618

type[23] : 2621

type[24] : 2624

type[25] : 2627

type[26] : 2630

type[27] : 4

type[28] : 4

type[29] : 4

type[30] : 4

[...]

type[5042] : 2

type[5043] : 2

type[5044] : 2

type[5045] : 2

type[5046] : 2

type[5047] : 2

type[5048] : 2

type[5049] : 2

type[5050] : 2

分享给朋友:
您可能感兴趣的文章:
随机阅读: