I have the following scenario in the C (!!! not C++ !!!) code:
#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. 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...
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. 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...
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;
which is the value that up
pointer holds and selecting ux
field out of that value
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...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.