#include<stdio.h>
#include<malloc.h>
typedef struct AStruct TypedefedStruct;
struct AStruct
{
int member;
};
void main()
{
/* Case 1
============================================================================
Use (->) operator to access structure member with structure pointer, instead
of dot (.) operator.
*/
struct AStruct *aStructObjPtr = (struct AStruct *)malloc(sizeof(struct AStruct));
//aStructObjPtr.member = 1; //Error: request for member ‘member’ in something not
//a structure or union.
//It should be as below.
aStructObjPtr->member = 1;
printf("%d",aStructObjPtr->member); //1
/* Case 2
============================================================================
We can use dot (.) operator with struct variable to access its members, but
not with with struct pointer. But we have to ensure we dont forget to wrap
pointer variable inside brackets.
*/
//*aStructObjPtr.member = 2; //Error, should be as below.
(*aStructObjPtr).member = 2;
printf("%d",(*aStructObjPtr).member); //2
/* Case 3
=============================================================================
Use (->) operator to access structure member with typedefed structure pointer,
instead of dot (.) operator.
*/
TypedefedStruct *typedefStructObjPtr = (TypedefedStruct *)malloc(sizeof(TypedefedStruct));
//typedefStructObjPtr.member=3; //Error, should be as below.
typedefStructObjPtr->member=3;
printf("%d",typedefStructObjPtr->member); //3
/* Case 4
============================================================================
We can use dot (.) operator with struct variable to access its members, but
not with with struct pointer. But we have to ensure we dont forget to wrap
pointer variable inside brackets.
*/
//*typedefStructObjPtr.member = 4; //Error, should be as below.
(*typedefStructObjPtr).member=4;
printf("%d",(*typedefStructObjPtr).member); //4
/* Case 5
============================================================================
We have to be extra carefull when dealing with pointer to pointers to
ensure that we follow all above rules.
We need to be double carefull while putting brackets around pointers.
*/
//5.1. Access via struct_ptrptr and ->
struct AStruct **aStructObjPtrPtr = &aStructObjPtr;
//*aStructObjPtrPtr->member = 5; //Error, should be as below.
(*aStructObjPtrPtr)->member = 5;
printf("%d",(*aStructObjPtrPtr)->member); //5
//5.2. Access via struct_ptrptr and .
//**aStructObjPtrPtr.member = 6; //Error, should be as below.
(**aStructObjPtrPtr).member = 6;
printf("%d",(**aStructObjPtrPtr).member); //6
//5.3. Access via typedefed_strct_ptrptr and ->
TypedefedStruct **typedefStructObjPtrPtr = &typedefStructObjPtr;
//*typedefStructObjPtrPtr->member = 7; //Error, should be as below.
(*typedefStructObjPtrPtr)->member = 7;
printf("%d",(*typedefStructObjPtrPtr)->member); //7
//5.4. Access via typedefed_strct_ptrptr and .
//**typedefStructObjPtrPtr->member = 8; //Error, should be as below.
(**typedefStructObjPtrPtr).member = 8;
printf("%d",(**typedefStructObjPtrPtr).member); //8
//5.5. All cases 5.1 to 5.4 will fail if you include incorrect number of *
// Below are examples of such usage of incorrect number *, correspnding
// to int values assigned to them
//(aStructObjPtrPtr)->member = 5; //Error
//(*aStructObjPtrPtr).member = 6; //Error
//(typedefStructObjPtrPtr)->member = 7; //Error
//(*typedefStructObjPtrPtr).member = 8; //Error
}
8条答案
按热度按时间nsc4cvqm1#
如果您尝试在有指针的情况下访问示例,也会发生这种情况,反之亦然:
正如在一个评论中指出的,如果有人使用
typedef
作为指针,也就是说,在typedef中包含*
,这可能会让人感到痛苦,如下所示:因为这样你得到的代码 * 看起来 * 像是在处理示例,而实际上它是在处理指针:
注意上面的代码看起来好像应该写成
a_foo.field
,但是那样会失败,因为Foo
是一个指向struct的指针。我强烈建议在C语言中使用而不是typedef
:艾德指针。指针很重要,不要隐藏你的星号。让它们发光。ffscu2ro2#
您正在尝试存取结构的成员,但却在非结构的项目中。例如:
zhte4eai3#
在以下情况下也可能发生:
例如,如果我们考虑堆栈的压入功能:
错误出现在push函数和注解行中。指针
s
必须包含在括号中。正确代码:kjthegm64#
我已经列举了可能所有的情况下,这个错误可能会发生在代码和它的注解如下。请添加到它,如果你遇到更多的情况。
潜在的想法是直的:
.
与结构变量一起使用。(情况2和4)->
并将指针指向结构。(情况1和3)(*ptr).
和(*ptr)->
与*ptr.
和*ptr->
(除情况1外的所有情况)lb3vh1jj5#
这可能意味着您忘记包含定义此结构/联合的头文件。例如:
foo.h文件:
main.c文件:
fae0ux8s6#
在以下情况下也会出现:
代替
oxf4rvwz7#
我在试着访问会员时看到了这个。
我的结构是这样的:
通常,我们将结构成员作为
我把testvar误认为是指针并执行了此操作。
就在那时我看到了这个错误。
对结构或联合以外的对象中的成员“a”的请求
of1yzvn48#
我可笑的经验是我错误地把“.”而不是“,”。