gpt4 book ai didi

c - 无法将正确的值写入 typedef 数组的指针

转载 作者:太空宇宙 更新时间:2023-11-04 06:07:01 26 4
gpt4 key购买 nike

我的问题如下 - 我有这行代码:

// allocate a new vector
Vector3I *theVector = (Vector3I*)calloc(1,sizeof(Vector3I));

// write face's points to this vector
*theVector[0] = a3dsFace->points[0];
*theVector[1] = a3dsFace->points[1];
*theVector[2] = a3dsFace->points[2];

points[] 数组中的值是 {0,1,2}。当我将它们写入我指向的 Vector3I 时,我得到了 {0,0,0}。你对我做错了什么有什么建议吗?

编辑:更多细节:

这是来自 lib3ds 的:http://code.google.com/p/lib3ds/

struct Lib3dsFace {
Lib3dsUserData user; /*! Arbitrary user data */
char material[64]; /*! Material name */
Lib3dsWord points[3]; /*! Indices into mesh points list */
Lib3dsWord flags; /*! See Lib3dsFaceFlag, below */
Lib3dsDword smoothing; /*! Bitmask; each bit identifies a group */
Lib3dsVector normal;
};

a3dsFace 是一个 Lib3dsFace 结构。

点数组来自这种类型:

 typedef unsigned __int16 Lib3dsWord

还有我的指针:

Vector3I* theVector

typedef int Vector3I[3];

我希望这能给问题带来一些启发。

谨致问候。

最佳答案

下面的代码可以工作并且是对您的代码片段的测试。如果某些东西不起作用,创建这样的测试可能是个好主意,使用 *a3dsFace 的硬编码值,以缩小问题范围。

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

typedef int Vector3I[3];
typedef uint16_t Lib3dsWord;

struct Lib3dsFace {
/* ... */
Lib3dsWord points[3]; /*! Indices into mesh points list */
/* ... */
};

/* ... */
struct Lib3dsFace some_face = { {0, 1, 2} };
struct Lib3dsFace *a3dsFace = &some_face;
/* ... */

int main(void)
{
Vector3I *theVector = (Vector3I*)calloc(1,sizeof(Vector3I));

(*theVector)[0] = a3dsFace->points[0];
(*theVector)[1] = a3dsFace->points[1];
(*theVector)[2] = a3dsFace->points[2];

printf("theVector: %p, *theVector: %p, &(*theVector)[0]: %p\n", theVector, *theVector, &(*theVector)[0]);

printf("RIGHT Addresses: %p, %p, %p\n", &(*theVector)[0], &(*theVector)[1], &(*theVector)[2]);
printf("WRONG Addresses: %p, %p, %p\n", &*theVector[0], &*theVector[1], &*theVector[2]);

printf("Values: %d, %d, %d\n", (*theVector)[0], (*theVector)[1], (*theVector)[2]);

free(theVector);

return 0;
}

输出:

theVector: 0x1cd3010, *theVector: 0x1cd3010, &(*theVector)[0]: 0x1cd3010
RIGHT Addresses: 0x1cd3010, 0x1cd3014, 0x1cd3018
WRONG Addresses: 0x1cd3010, 0x1cd301c, 0x1cd3028
Values: 0, 1, 2

我把地址放在那里,以便您可以看到 (*theVector)[0] 是访问动态分配的 Vector3I 的第一个元素的有效方法。

也许您没有正确设置 a3dsFace->points,这就是复制 {0, 0, 0} 的原因。另请注意,Vector3I 的每个元素都是 int 类型,并且每个点都是 uint16_t 类型。您也不需要使用 calloc 来将分配的内存清零,因为您是在为它们赋值之后立即进行的;你可以只使用 malloc

底线是您仍然没有提供足够的代码来找到您的确切问题,您应该添加代码以在代码中调试代码。

编辑: 我不小心得到了 *theVector[0],它应该是并且现在是 (*theVector)[0],因为[] 的优先级高于 *。否则它会导致未定义的行为,因为你正在越过数组的边界,我的错。我不知道我是怎么忘记的,这是我要在您进行编辑之前发布答案的主要原因之一。它工作正常,但如果您通过 valgrind 之类的程序运行它,它会告诉您有些地方不太正确(即使它可能按预期运行)。

从上面输出的地址可以看出,有很大的不同。例如 *theVector[1],由于运算符优先级与 *(theVector[1]) 相同,这意味着它将递增指向的地址通过 theVector 通过 3 * sizeof(int) 字节(又名 sizeof(Vector3I)),而不只是 1 * sizeof( int)(*theVector)[1] 的(正确)情况下。

关于c - 无法将正确的值写入 typedef 数组的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8406487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com