C语言 在结构方法/函数中创建的值与在main中访问的值不同

cyej8jka  于 2023-01-01  发布在  其他
关注(0)|答案(1)|浏览(115)

我是C新手,两天前开始的。有一些Python的经验。
我所做的是创建一个结构来表示一个3D的Point,它的值是指向一个浮点数的指针,关于它不是浮点数的原因,我们会在后面解释。
我还创建了一个Face结构,它有多个点。Face是一个三角形,所以有3个点和一个中心点,位于三角形的中心。同样,它使用指针。
最后(在看不见的代码中)我将有一个3D对象结构。它将拥有一个顶点矩阵,一个面顶点索引数组和一个指向面的指针数组。
因为它使用指针,所以如果我平移对象、触摸单个点或修改点坐标,它将适用于所有对象。

虫子

我计算了Face的中心点,给它分配了一个Point 3D,给它分配了一个指针。
当我在方法内部打印Point 3D时,它可以工作。当我在main中通过指针打印它时,它会给出一个默认/随机生成的浮点数,就像它从未被赋值一样。我尝试了以下代码:

#include <stdio.h>
#include <stdlib.h>

//---------------------------------Point3D structure.----------------------------------------
typedef struct
{
    float *x;
    float *y;
    float *z;
} Point3D; // using pointers

Point3D *print_Point3D(Point3D *point)
{
    printf("p[x y z] = [ %p %p %p ]\n", (point->x), (point->y), (point->z));
    printf("[x y z] = [ %f %f %f ]\n", *(point->x), *(point->y), *(point->z));
}

//---------------------------------PolygonFace structure----------------------------------------
typedef struct
{
    Point3D *p1;
    Point3D *p2;
    Point3D *p3;
    Point3D *center;
} PolygonFace;

PolygonFace *createPolygonFace(PolygonFace *face,
                               Point3D *pPoint1, Point3D *pPoint2, Point3D *pPoint3)
{
    face = malloc(sizeof(pPoint1) * 4);
    face->p1 = pPoint1;
    face->p2 = pPoint2;
    face->p3 = pPoint3;

    float center_x, center_y, center_z;
    center_x = center_y = center_z = 0.f;

    center_x = (*(pPoint1->x) + *(pPoint2->x) + *(pPoint3->x)) / (3.f);
    center_y = (*(pPoint1->y) + *(pPoint2->y) + *(pPoint3->y)) / (3.f);
    center_z = (*(pPoint1->z) + *(pPoint2->z) + *(pPoint3->z)) / (3.f);

    Point3D center_point = {.x = &center_x, .y = &center_y, .z = &center_z};
    face->center = &center_point;
    printf("This is the center point inside:\n");
    print_Point3D(face->center);

    return face;
}

//---------------------------------------Main-------------------------
void main()
{
    printf("----------------start of program--------------\n\n");

    float x1, x2, x3, y1, y2, y3, z1, z2, z3;
    x2 = x3 = y1 = y3 = z1 = z2 = 0;
    x1 = y2 = z3 = 1;

    // p1 = [1, 0 ,0 ]
    // p2 = [0,1,0]
    // p3 = [0,0,1] 

    Point3D po1 = {.x = &x1, .y = &y1, .z = &z1};
    Point3D po2 = {.x = &x2, .y = &y2, .z = &z2};
    Point3D po3 = {.x = &x3, .y = &y3, .z = &z3};

    PolygonFace *face_test = createPolygonFace(face_test, &po1, &po2, &po3);

    printf("\n This is the center point\n");
    print_Point3D((face_test->center));
}

我来吧

我期望它们相等。[0.33 0.33 0.33]三角形的中心位于i,^j,^k。

dxxyhpgq

dxxyhpgq1#

createPolygonFace中,你要捕获分配结构中几个局部变量的地址,并返回这些局部指针。当函数退出时,这些地址的生存期结束,使指针无效。试图解引用这些无效指针会触发undefined behavior
你需要为指针指向的每一项分配内存,也不需要第一个参数,因为你无论如何都要覆盖它的值:

PolygonFace *createPolygonFace(Point3D *pPoint1, Point3D *pPoint2, Point3D *pPoint3)
{
    PolygonFace *face = malloc(sizeof(PolygonFace));
    face->p1 = pPoint1;
    face->p2 = pPoint2;
    face->p3 = pPoint3;

    float *center_x, *center_y, *center_z;
    center_x = malloc(sizeof(float));
    center_y = malloc(sizeof(float));
    center_z = malloc(sizeof(float));

    *center_x = (*(pPoint1->x) + *(pPoint2->x) + *(pPoint3->x)) / (3.f);
    *center_y = (*(pPoint1->y) + *(pPoint2->y) + *(pPoint3->y)) / (3.f);
    *center_z = (*(pPoint1->z) + *(pPoint2->z) + *(pPoint3->z)) / (3.f);

    Point3D *center_point = malloc(sizeof(Point3D));
    center_point->x = center_x;
    center_point->y = center_y;
    center_point->z = center_z;

    face->center = center_point;
    printf("This is the center point inside:\n");
    print_Point3D(face->center);

    return face;
}

但是请注意,您的PolygonFace示例包含指向某些已分配对象的指针和指向本地对象的指针,您无法知道哪个是哪个。
考虑到您是C语言的新手,我强烈建议您完全取消指针,直到您更好地掌握它们是如何工作的以及如何管理它们。

相关问题