2016-03-16 33 views
0

我的结构是这样的:如何获得一个结构部件上指针的结构体内部

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; 
}; 

我initiliaze他们的方式(在我的主):

HelperGlpk helper_glpk; 

    helper_glpk.matrix_coefs = malloc((nbr_coefs + 1) * sizeof(double)); 
    helper_glpk.matrix_coefs[0] = 0; 

    helper_glpk.obj_coefs = malloc((nbr_colums + 1) * sizeof(double)); 
    helper_glpk.obj_coefs[0] = 0; 

    helper_glpk.column_of_coef = malloc((nbr_colums + 1) * sizeof(int)); 
    helper_glpk.column_of_coef[0] = 0; 

    helper_glpk.row_of_coef = malloc((nbr_rows + 1) * sizeof(int)); 
    helper_glpk.row_of_coef[0] = 0; 

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

    helper_glpk.cpt_coef = 1; 
    helper_glpk.cpt_contrainte = 1; 

然后,在函数内部genere_contrainte_1(),我称之为是这样的:genere_contrainte_1(i, j, &helper_glpk, baie);

我要访问指针helper_glpk->col_bounds[helper_glpk->cpt_coef]->type但我得到这个错误:

error: invalid type argument of ‘->’ (have ‘Bounds {aka struct Bounds}’) 
    helper_glpk->col_bounds[helper_glpk->cpt_coef]->type = GLP_DB; 

你能告诉我我做错了什么吗?

编辑:I DO要访问的指针 - >类型,因为.TYPE不“保存”的值的函数genere_contrainte_1()

+0

helper_glpk是一个结构,所以你应该使用'.'操作,而不是' - >'运营商 – Taelsin

+0

问题应显示函数原型' genere_contrainte_1'功能。 – user3386109

+1

作为对编辑的回应:您需要发布[最小完整可验证示例](http://stackoverflow.com/help/mcve)。目前还不清楚你的意思是什么*“。type不保存在函数外部使用的值”*。这应该。 – user3386109

回答

0

HelperGlpk的结构的col_bounds构件正被使用外用作数组。这是说,

helper_glpk->col_bounds[helper_glpk->cpt_coef] 

Bounds结构的一个实例,一个指向Bounds结构。

因此,正确的语法使用点符号

helper_glpk->col_bounds[helper_glpk->cpt_coef].type = GLP_DB; 
+0

我的问题是我想访问键入的指针,因为我在一个函数内。 (我编辑了我的文章) – Hayanno

+0

然后你需要显示功能。 – user3386109

0

helper_glpk->col_bounds计算结果为Bounds*

helper_glpk->col_bounds[helper_glpk->cpt_coef]评估为Bounds

因此,你需要使用:

helper_glpk->col_bounds[helper_glpk->cpt_coef].type = GLP_DB; 
              // ^^ Use . not -> 
+0

我的问题是我想访问键入的指针,因为我在一个函数内。 – Hayanno