我有一个像这样的结构
struct T {
int *baseofint
}Tstruct, *pTstruct;
int one;
pTstruct pointer;现在我想定义一下
one = pointer.baseofint; //got filled with an integer;
error message: **operator is not equal to operand**我也试过了
one = *(pointer.baseofint);
error message:**operand of * is no pointer*也许有人能帮上忙谢谢。
发布于 2011-11-05 04:18:48
首先,我不认为下面的代码是你想的那样:
struct T {
int *baseofint
}Tstruct, *pTstruct;
int one;
pTstruct pointer;您将声明一个结构类型struct T,并创建它的一个名为Tstruct的实例和一个名为pTstruct的指向它的指针。但这些不是你正在创建的类型,它们是变量。这也使得pTstruct pointer;代码无效。您可能想要的是一个类型定义函数:
typedef struct T {
int *baseofint;
} Tstruct, *pTstruct;...to使Tstruct等同于struct T,pTstruct等同于struct T *。
至于访问和取消对baseofint字段的引用,根据您是否通过指针访问它而略有不同...但这是如何做到的:
Tstruct ts; // a Tstruct instance
pTstruct pts = &ts; // a pTstruct -- being pointed at ts
/* ...some code that points ts.baseofint at
* an int or array of int goes here... */
/* with * operator... */
int one = *(ts.baseofint); // via struct, eg. a Tstruct
int oneb = *(pts->baseofint); // via pointer, eg. a pTstruct
/* with array brackets... */
int onec = ts.baseofint[0]; // via Tstruct
int oned = pts->baseofint[0]; // via pTstruct发布于 2011-11-05 02:34:50
pTstruct是指向结构的指针。该结构包含一个指向int的指针。因此,您需要同时取消对它们的引用。尝试:
*((*pointer).baseofint)另请注意
p->x是的缩写
(*p).x所以
*(pointer->baseofint)也是有效的(并且不太难读)。
发布于 2011-11-05 02:32:18
您可能需要*(pointer->baseofint)。
https://stackoverflow.com/questions/8014104
复制相似问题