错误:请求成员“y”在某种非结构或联合中。

时间:2022-01-14 21:21:16

I have problem with compilation.

我对编译有问题。

This is my main:

这是我主要的:

int main(void) {

    TP_Init();

    Coordinate *dotyk;

    while( 1 )
    {
        dotyk = Read_Ads7846();

        int xx = *dotyk.x;
        int yy = *dotyk.y;

        TP_DrawPoint(xx,yy);
    }
}

My function Read_Ads looks:

我的函数Read_Ads看起来:

Coordinate *Read_Ads7846(void) {

    static Coordinate  screen;
    int m0,m1,m2,TP_X[1],TP_Y[1],temp[3];
    uint8_t count=0;
    int buffer[2][9]={{0},{0}};  
    do                         
    {          
        TP_GetAdXY(TP_X,TP_Y);  
        buffer[0][count]=TP_X[0];  
        buffer[1][count]=TP_Y[0];
        count++;  
    }
    while(!TP_INT_IN&& count<9);  
    if(count==9)  
    {  
        temp[0]=(buffer[0][0]+buffer[0][1]+buffer[0][2])/3;
        temp[1]=(buffer[0][3]+buffer[0][4]+buffer[0][5])/3;
        temp[2]=(buffer[0][6]+buffer[0][7]+buffer[0][8])/3;

        m0=temp[0]-temp[1];
        m1=temp[1]-temp[2];
        m2=temp[2]-temp[0];

        m0=m0>0?m0:(-m0);
        m1=m1>0?m1:(-m1);
        m2=m2>0?m2:(-m2);
        if( m0>THRESHOLD  &&  m1>THRESHOLD  &&  m2>THRESHOLD )
            return 0;
        if(m0<m1)
        {
            if(m2<m0) 
                screen.x=(temp[0]+temp[2])/2;
            else 
                screen.x=(temp[0]+temp[1])/2;   
        }
        else if(m2<m1) 
          screen.x=(temp[0]+temp[2])/2;
        else 
          screen.x=(temp[1]+temp[2])/2;

        temp[0]=(buffer[1][0]+buffer[1][1]+buffer[1][2])/3;
        temp[1]=(buffer[1][3]+buffer[1][4]+buffer[1][5])/3;
        temp[2]=(buffer[1][6]+buffer[1][7]+buffer[1][8])/3;
        m0=temp[0]-temp[1];
        m1=temp[1]-temp[2];
        m2=temp[2]-temp[0];
        m0=m0>0?m0:(-m0);
        m1=m1>0?m1:(-m1);
        m2=m2>0?m2:(-m2);
        if(m0>THRESHOLD&&m1>THRESHOLD&&m2>THRESHOLD)
            return 0;

        if(m0<m1)
        {
            if(m2<m0) 
                screen.y=(temp[0]+temp[2])/2;
            else 
                screen.y=(temp[0]+temp[1])/2;   
        }
        else if(m2<m1) 
            screen.y=(temp[0]+temp[2])/2;
        else
            screen.y=(temp[1]+temp[2])/2;

        return &screen;
    }  
    return 0; 
}

I got the error:

我得到了错误:

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

请求成员“y”,而不是结构或工会。

I thought I need to use -> operator but it causes error too.

我想我需要使用>操作符,但它也会导致错误。

Can you explain what is wrong ?

你能解释一下什么是错的吗?

3 个解决方案

#1


0  

You need -> instead . because of structure pointer and remove * before it.

你需要->。因为结构指针和删除*在它之前。

it should be

它应该是

int xx = dotyk->x;
int yy = dotyk->y;

#2


0  

Jayesh is right with his answer, but to clarify things I think it's important that you know the difference between your code and his answer.

Jayesh对他的回答是正确的,但要澄清我认为重要的是你要知道你的代码和他的答案之间的区别。

Your problem is you assumed that * operator has higher precedence than ".". And that's wrong assumption.

你的问题是你假定*运算符的优先级高于“。”这是错误的假设。

If you put (*dotyk).y it woult work as fine as ->.

如果你把(* dotyk)。它不会像>那样好。

"->" is just a shortcut for (*something).member.

“->”只是(*某物)的一个快捷方式。

#3


0  

The variable dotyk is a Coordinate (which is a typedef of a struct) pointer. Now, when you try to access your variable variables (the variables that are in that structure) you did so:

变量dotyk是一个坐标(它是一个struct的typedef)指针。现在,当你尝试访问变量变量(在那个结构中的变量)时,你这样做了:

int yy = *dotyk.y;

The problem is that as there are mathmatic operators with order of doing each (* and / before + and `- for example) so there is an order in C operators.

问题是,由于有mathmatic操作符执行每个操作(*和/ before +和),所以在C操作符中有一个顺序。

In C, . goes before * so what you are doing is actually calling a variable y in the address of the pointer ITSELF (dotyk) and not the address of WHERE IT IS POINTING TO (the structure).

在C语言中,。在*之前,你所做的实际上是在指针本身的地址(dotyk)中调用变量y,而不是指向它指向的地址(结构)。

There are two solution:

有两种解决方案:

  1. Use brackets.

    使用括号。

    int yy = *dotyk.y; turns to

    int yy = * dotyk.y;转向

    int yy = (*dotyk).y; so that you'll mention that you want the operator * to be performed first so the Y variable request would be performed on the content of the pointer (where it is pointing to) and not the pointer itself.

    int yy =(* dotyk).y;因此,您将会提到您希望操作符*首先执行,以便在指针的内容上执行Y变量请求(它指向的位置),而不是指针本身。

  2. Use -> operator.

    使用- >操作符。

The -> operator is very useful when it comes to structure pointers.

当涉及到结构指针时,->操作符非常有用。

Instead of writing int yy = *dotyk.y; you would just write int yy = dotyk->y; and that would say something like "Hey, I want you to treat dotyk as a pointer and lead me to the Y variable that can be found where it is pointing towards".

而不是int yy = *dotyk.y;你只需要写int yy = dotyk->y;这就像“嘿,我要你把dotyk当作一个指针,引导我找到指向它的Y变量。”

Hope I've helped.

希望我帮助。

#1


0  

You need -> instead . because of structure pointer and remove * before it.

你需要->。因为结构指针和删除*在它之前。

it should be

它应该是

int xx = dotyk->x;
int yy = dotyk->y;

#2


0  

Jayesh is right with his answer, but to clarify things I think it's important that you know the difference between your code and his answer.

Jayesh对他的回答是正确的,但要澄清我认为重要的是你要知道你的代码和他的答案之间的区别。

Your problem is you assumed that * operator has higher precedence than ".". And that's wrong assumption.

你的问题是你假定*运算符的优先级高于“。”这是错误的假设。

If you put (*dotyk).y it woult work as fine as ->.

如果你把(* dotyk)。它不会像>那样好。

"->" is just a shortcut for (*something).member.

“->”只是(*某物)的一个快捷方式。

#3


0  

The variable dotyk is a Coordinate (which is a typedef of a struct) pointer. Now, when you try to access your variable variables (the variables that are in that structure) you did so:

变量dotyk是一个坐标(它是一个struct的typedef)指针。现在,当你尝试访问变量变量(在那个结构中的变量)时,你这样做了:

int yy = *dotyk.y;

The problem is that as there are mathmatic operators with order of doing each (* and / before + and `- for example) so there is an order in C operators.

问题是,由于有mathmatic操作符执行每个操作(*和/ before +和),所以在C操作符中有一个顺序。

In C, . goes before * so what you are doing is actually calling a variable y in the address of the pointer ITSELF (dotyk) and not the address of WHERE IT IS POINTING TO (the structure).

在C语言中,。在*之前,你所做的实际上是在指针本身的地址(dotyk)中调用变量y,而不是指向它指向的地址(结构)。

There are two solution:

有两种解决方案:

  1. Use brackets.

    使用括号。

    int yy = *dotyk.y; turns to

    int yy = * dotyk.y;转向

    int yy = (*dotyk).y; so that you'll mention that you want the operator * to be performed first so the Y variable request would be performed on the content of the pointer (where it is pointing to) and not the pointer itself.

    int yy =(* dotyk).y;因此,您将会提到您希望操作符*首先执行,以便在指针的内容上执行Y变量请求(它指向的位置),而不是指针本身。

  2. Use -> operator.

    使用- >操作符。

The -> operator is very useful when it comes to structure pointers.

当涉及到结构指针时,->操作符非常有用。

Instead of writing int yy = *dotyk.y; you would just write int yy = dotyk->y; and that would say something like "Hey, I want you to treat dotyk as a pointer and lead me to the Y variable that can be found where it is pointing towards".

而不是int yy = *dotyk.y;你只需要写int yy = dotyk->y;这就像“嘿,我要你把dotyk当作一个指针,引导我找到指向它的Y变量。”

Hope I've helped.

希望我帮助。