C 如何对指向结构的指针执行操作?

C 如何对指向结构的指针执行操作?,c,function,pointers,struct,C,Function,Pointers,Struct,我有一个C程序,它有多个函数,可以在矩形上进行不同的计算。为了映射矩形和矩形的点,我定义了结构。我被一个函数(moveR)卡住了,它接受一个矩形指针和两个int值作为参数,然后根据这些参数移动矩形 [![enter image description here][1][1]我遇到的问题是该函数不更新新位置。例如,在我对“moveR()”的调用中,它应该将矩形左上角的x值移动到18,但它一直返回15。任何帮助都将不胜感激 #include <stdio.h> struct p

我有一个C程序,它有多个函数,可以在矩形上进行不同的计算。为了映射矩形和矩形的点,我定义了结构。我被一个函数(moveR)卡住了,它接受一个矩形指针和两个int值作为参数,然后根据这些参数移动矩形

[![enter image description here][1][1]我遇到的问题是该函数不更新新位置。例如,在我对“moveR()”的调用中,它应该将矩形左上角的x值移动到18,但它一直返回15。任何帮助都将不胜感激

#include <stdio.h>

    struct point{
        int x, y;
    } point1, point2, answer, point1b, point2b;
    
    struct rectangle{
        struct point upperLeft, lowerRight;
    } R1;
    
    struct point centerR (struct rectangle R1){
        int centerx= (R1.lowerRight.x-R1.upperLeft.x)/2;
        int centery= (R1.lowerRight.y-R1.upperLeft.y)/2;
        struct point pointR;
        pointR.x= centerx;
        pointR.y= centery;
        return pointR;
    } 
    
    struct rectangle moveR(struct rectangle *R1, int x, int y){
        R1->upperLeft.x+x;
        R1->lowerRight.x+x;
        R1->upperLeft.y+y;
        R1->lowerRight.y+y;
        
        return *R1;
    }
    
    struct rectangle *R2;
    
    
int main()
{
    point1.x= 6;
    point1.y= 6;
    point2.x= 10;
    point2.y= 12;
    
    R1.upperLeft= point1;
    R1.lowerRight= point2;
    
    answer = centerR(R1);
    
    printf("The center of R1 is:\n");
    printf("The x value of the center is %d \n", answer.x);
    printf("The y value of the center is %d \n", answer.y);
    
    R2=  &R1;
    point1b.x=15;
    point1b.y=15;
    point2b.x=25;
    point2b.y=25;
    R2->upperLeft = point1b;
    R2->lowerRight= point2b;
    
    moveR(R2,3,4);
    
    printf("The position of R2 is:\n");
    printf("The x value of the upper left is %d \n", R1.upperLeft.x);
    
    return 0;
}```


  [1]: https://i.stack.imgur.com/I2pO5.png
#包括
结构点{
int x,y;
}第1点,第2点,答案,第1b点,第2b点;
结构矩形{
结构点左上,右下;
}R1;
结构点中心(结构矩形R1){
int centerx=(R1.lowerRight.x-R1.upperLeft.x)/2;
int centery=(R1.右下角y-R1.左上角y)/2;
结构点指针;
pointR.x=centerx;
点y=中心y;
返回指针;
} 
结构矩形移动器(结构矩形*R1,整数x,整数y){
R1->左上角.x+x;
R1->lowerRight.x+x;
R1->左上方。y+y;
R1->右下角y+y;
返回*R1;
}
结构矩形*R2;
int main()
{
点1.x=6;
点1.y=6;
点2.x=10;
点2.y=12;
R1.左上=点1;
R1.lowerRight=点2;
答案=center(R1);
printf(“R1的中心是:\n”);
printf(“中心的x值为%d\n”,答案为.x);
printf(“中心的y值为%d\n”,答案为.y);
R2=&R1;
点1b.x=15;
点1b.y=15;
点2b.x=25;
点2b.y=25;
R2->左上=点1b;
R2->lowerRight=点2B;
移动器(R2,3,4);
printf(“R2的位置为:\n”);
printf(“左上角的x值为%d\n”,R1.upperLeft.x);
返回0;
}```
[1]: https://i.stack.imgur.com/I2pO5.png
您可以在此处进行计算:

R1->upperLeft.x+x;
R1->lowerRight.x+x;
R1->upperLeft.y+y;
R1->lowerRight.y+y;
那么,结果保存在哪里呢?您是否打算将更新后的结果保存在R1中,如下所示

R1->upperLeft.x += x;
R1->lowerRight.x += x;
R1->upperLeft.y += y;
R1->lowerRight.y += y;

R1->upperLeft.x+x的结果在哪里开始?在我看来,当我调用它时,它应该更新R2的值。我认为应该有R1->upperLeft.x+=x;或者R1->upperLeft.x=R1->upperLeft.x+x;谢谢你的工作!