gpt4 book ai didi

What does "request for member '*******' in something not a structure or union" mean?(“请求成员‘*’不是结构或联盟”是什么意思?)

转载 作者:bug小助手 更新时间:2023-10-25 14:52:25 25 4
gpt4 key购买 nike



Is there an easy explanation for what this error means?

这个错误意味着什么,有什么简单的解释吗?



request for member '*******' in something not a structure or union


I've encountered it several times in the time that I've been learning C, but I haven't got a clue as to what it means.

在我学习C语言的这段时间里,我已经遇到过它好几次了,但我对它的含义一无所知。


更多回答
优秀答案推荐

It also happens if you're trying to access an instance when you have a pointer, and vice versa:

如果您试图在拥有指针的情况下访问实例,也会发生这种情况,反之亦然:



struct foo
{
int x, y, z;
};

struct foo a, *b = &a;

b.x = 12; /* This will generate the error, should be b->x or (*b).x */


As pointed out in a comment, this can be made excruciating if someone goes and typedefs a pointer, i.e. includes the * in a typedef, like so:

正如在一条评论中所指出的,如果有人去定义一个指针,即在类型定义函数中包括*,这可能会令人痛苦,如下所示:



typedef struct foo* Foo;


Because then you get code that looks like it's dealing with instances, when in fact it's dealing with pointers:

因为这样的代码看起来像是在处理实例,而实际上它是在处理指针:



Foo a_foo = get_a_brand_new_foo();
a_foo->field = FANTASTIC_VALUE;


Note how the above looks as if it should be written a_foo.field, but that would fail since Foo is a pointer to struct. I strongly recommend against typedef:ed pointers in C. Pointers are important, don't hide your asterisks. Let them shine.

注意上面的代码看起来好像应该写一个_foo.field,但是这样做会失败,因为foo是一个指向struct的指针。我强烈建议不要使用tyecif:在C中使用Ed指针。指针很重要,不要隐藏星号。让他们发光吧。



You are trying to access a member of a structure, but in something that is not a structure. For example:

您正在尝试访问结构的成员,但访问的对象不是结构。例如:



struct {
int a;
int b;
} foo;
int fum;
fum.d = 5;


It may also happen in the following case:

在以下情况下也可能发生这种情况:



eg. if we consider the push function of a stack:

例如。如果我们考虑堆栈的推送功能:



typedef struct stack
{
int a[20];
int head;
}stack;

void push(stack **s)
{
int data;
printf("Enter data:");
scanf("%d",&(*s->a[++*s->head])); /* this is where the error is*/
}

main()
{
stack *s;
s=(stack *)calloc(1,sizeof(stack));
s->head=-1;
push(&s);
return 0;
}


The error is in the push function and in the commented line. The pointer s has to be included within the parentheses. The correct code:

错误出现在PUSH函数和注释行中。指针S必须包括在括号内。正确的代码:



scanf("%d",&( (*s)->a[++(*s)->head]));


I have enumerated possibly all cases where this error may occur in code and its comments below. Please add to it, if you come across more cases.

我已经在下面列举了在代码及其注释中可能发生此错误的所有情况。如果你遇到更多的情况,请补充一下。



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


The underlying ideas are straight:

潜在的想法是直截了当的:




  • Use . with structure variable. (Cases 2 and 4)

  • Use -> with pointer to structure. (Cases 1 and 3)

  • If you reach structure variable or pointer to structure variable by following pointer, then wrap the pointer inside bracket: (*ptr). and (*ptr)-> vs *ptr. and *ptr-> (All cases except case 1)

  • If you are reaching by following pointers, ensure you have correctly reached pointer to struct or struct whichever is desired. (Case 5, especially 5.5)



It may means that you forgot include a header file that define this struct/union.
For example:

这可能意味着您忘记了包含定义此结构/联合的头文件。例如:



foo.h file:

Foo.h文件:



typedef union
{
struct
{
uint8_t FIFO_BYTES_AVAILABLE : 4;
uint8_t STATE : 3;
uint8_t CHIP_RDY : 1;
};
uint8_t status;
} RF_CHIP_STATUS_t;

RF_CHIP_STATUS_t getStatus();


main.c file:

Main.c文件:



.
.
.
if (getStatus().CHIP_RDY) /* This will generate the error, you must add the #include "foo.h" */
.
.
.


can also appear if:

也可以在以下情况下出现:



struct foo {   int x, int y, int z }foo; 

foo.x=12


instead of

而不是



struct foo {   int x; int y; int z; }foo; 

foo.x=12


I saw this when I was trying to access the members.

当我试图访问会员时,我看到了这一点。


My struct was this:

我的结构是这样的:


struct test { 
int a;
int b;
};

struct test testvar;

Normally we access structure members as

通常,我们以如下方式访问结构成员


testvar.a;
testvar.b;

I mistook testvar to be a pointer and did this.

我误以为Testvar是一个指针,于是做了这件事。


testvar->a;

That's when I saw this error.

就在那时,我看到了这个错误。



request for member ‘a’ in something not a structure or union




My ridiculous experience is that I incorrectly put '.' instead of ','.

我可笑的经历是,我错误地把‘’而不是‘,’。


printf("%c". ch);


https://stackoverflow.com/a/42668014/21290212

Https://stackoverflow.com/a/42668014/21290212


the above is a answer from this same question. I would like to add a case which I encountered.

以上就是这个问题的答案。我想补充一个我遇到的案例。


typedef struct Node{
int data;
Node *next;
} Node;

this code won't work, and I got the same error as discussed in the question,

此代码不起作用,我收到了与问题中讨论的相同的错误,


the solution I found is below

我找到的解决方案如下


typedef struct Node{
int data;
struct Node *next;
} Node;

or


typedef struct Node Node;
struct Node{
int data;
Node *next;
};

I believe it is because compiler does not know what is typedefedStructure Node before the typedef is provided.

我相信这是因为编译器在提供typlef之前不知道什么是typefefdStructure Node。


更多回答

I'll bet this is the actual problem. It still bites me on occasion, especially if someone has typedef'd a pointer type.

我打赌这就是真正的问题所在。它有时仍然让我头疼,特别是当有人定义了指针类型的类型时。

I'd just add that this error will appear if an array has not been allocated (malloc) and is accessed.

我要补充的是,如果数组尚未分配(Malloc)并被访问,则会出现此错误。

I know it's been about a decade or more since this was posted, but those last two sentences made this my new favourite post. "Pointers are important, don't hide your asterisks. Let them shine."

我知道这篇帖子已经发布了大约十年或更长时间,但最后两句话使这篇帖子成为我最喜欢的新帖子。“指针很重要,不要隐藏你的星号。让它们发光吧。”

Thank you for making the pointer point (no terrible play on language) explicit. Other answers mentioned it (eg "let your pointers shine") but at 2am struggling in an epic battle with GDB and Valgrind, people such as myself appreciate that your answer explicitly shows how the pointer can be an issue and how that issue can be rectified.

感谢您让指针(没有糟糕的语言游戏)变得清晰。其他的回答都提到了(例如“让你的指针发光”),但在凌晨2点与GDB和Valgrind的一场史诗般的战斗中挣扎的时候,像我这样的人很感激你的答案明确地表明了指针可能会成为一个问题,以及如何纠正这个问题。

There is no difference between these two statements?

这两种说法没有区别吗?

@AlaaM. looking back on it months later, I missed the semicolons!

@AlaaM。几个月后回想起来,我错过了分号!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com