简体   繁体   English

从指针分配值到变量失败

[英]assigning value from pointer to variable fails

I have the following scenario in the C (!!! not C++ !!!) code: 我在C(!!!不是C ++ !!!)代码中有以下情形:

#include <stdlib.h>

struct point
{
    double *x, *y;
};

void point_Construct(struct point *p)
{
    p->x = (double*)malloc(sizeof(double));
    p->y = (double*)malloc(sizeof(double));
}

struct point3D
{
    double *ux, *uy, *uz;
};

void point3D_Construct(struct point3D *uP, double *x, double *y, double *z)
{
    uP->ux = x; //assigning pointers to pointers
    uP->uy = y;
    uP->uz = z;
}

void point3D_Compute(struct point3D *uP)
{
    double cx, cy, cz;

    //the following 3 lines do not work...
    //i.e.,  *uP->ux contains the right value but after assigning this value
    //to the cx variable, the cx holds some unreasonable value...
    cx = *uP->ux; //assigning values to which the pointers points to local variables
    cy = *uP->uy;
    cz = *uP->uz;

    cz = cx + cy; //using values

    //... other code...
}

static struct point  instPoint;  //create structures
static struct point3D instPoint3D;

static double mx, my, mz; //declare global variables

int main(void)
{

    mx = 1.0; //assigning values to static variables
    my = .0;
    mz = 24.5;

    point_Construct(&instPoint); //alloc memory for struct point

    //assigning values to the place in memory where pointers of the point struct point
    *instPoint.x = mx;
    *instPoint.y = my;

    //inicialize pointers of the point3D struct to memory addresses 
    //pointed by the pointers of the point struct and
    //to the address of the mz static global variable
    point3D_Construct(&instPoint3D, instPoint.x, instPoint.y, &mz);


    point3D_Compute(&instPoint3D); //using all the values

    //...other code...
}

The code compiles without any issue. 该代码编译没有任何问题。 The problem is within the point3D_Compute function. 问题出在point3D_Compute函数内。 I can see in debugger that values to which the pointers point are correct. 我可以在调试器中看到指针指向的值是正确的。 After assigning this values to the local double variables, these variables contain some garbage values instead of the correct ones... 将这些值分配给局部double变量后,这些变量包含一些垃圾值,而不是正确的值...

I've already tried the following methods but no one of them is working: 我已经尝试了以下方法,但是没有一个在工作:

cx = *up->ux;

or 要么

cx = *(up->ux);

or 要么

cx = up->ux[0];

What am I missing? 我想念什么?

Thank you in advance for any help... 预先感谢您的任何帮助...

The code compiles without any issue. 该代码编译没有任何问题。 The problem is within the point3D_Compute function. 问题出在point3D_Compute函数内。 I can see in debugger that values to which the pointers point are correct. 我可以在调试器中看到指针指向的值是正确的。 After assigning this values to the local double variables, these variables contain some garbage values instead of the correct ones... 将这些值分配给局部double变量后,这些变量包含一些垃圾值,而不是正确的值...

I've already tried the following methods but no one of them is working: 我已经尝试了以下方法,但是没有一个在工作:

cx = *up->ux;

or 要么

cx = *(up->ux);

or 要么

cx = up->ux[0];

What am I missing? 我想念什么?

Thank you in advance for any help... 预先感谢您的任何帮助...

try *(up)->ux; 尝试*(up)->ux; which is the value that up pointer holds and selecting ux field out of that value 这是up指针持有的值,并从该值中选择ux字段

Even I do not understand why it is not working by the standard way, I've found the working solution: 即使我不明白为什么它不能按标准方式工作,我也找到了可行的解决方案:

void point3D_Compute(struct point3D *uP)
{
    double cx, cy, cz;
    double *pCx, *pCy;

    pCx = &cx;
    pCy = &cy;    

    pCx = uP->ux; 
    pCy = uP->uy;

    cz = cx + cy; //it is OK now...

    //... other code...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM