typedef 结构 {
float 位置[3];
float 颜色[4];
float 顶点法线[3];
顶点;
typedef 结构 WingedEdge{
结构 WingedEdge* sym;
结构 WingedEdge* 接下来;
结构 WingedEdge* 上一个;
顶点** 顶点;
GLushort** 索引指针;
翼缘;
顶点* 顶点;
GLushort* 指数;
结构 WingedEdge* wingedEdges;
int 顶点数;//在其他地方初始化
int 索引数;//在其他地方初始化,它乘以三,因为我没有使用结构体作为索引
顶点 = (顶点 *) malloc(顶点数 * sizeof(顶点));
索引 = (GLushort *) malloc(numberOfIndices * sizeof(GLushort) * 3);
wingedEdges = (struct WingedEdge*)malloc(sizeof(struct WingedEdge)*numberOfIndices*3);
for (int i = 0; i < numberOfIndices*3; i+=3) {
wingedEdges[i].indexPointer = (&indices+i);
wingedEdges[i+1].indexPointer = (&indices+i);
wingedEdges[i+2].indexPointer = (&indices+i);
wingedEdges[i].vertex = (&vertices+indices[i]);
wingedEdges[i+1].vertex = (&vertices+indices[i+1]);
wingedEdges[i+2].vertex = (&vertices+indices[i+2]);
NSLog(@"%hu %hu %hu", *(索引+i),*(索引+i+1),索引[i+2]);
NSLog(@"%f %f %f", (顶点+索引[i])->位置[0], (顶点+索引[i])->位置[1], (顶点+索引[i]) ->位置[2]);
NSLog(@"%f %f %f", (顶点+索引[i+1])->位置[0], (顶点+索引[i+1])->位置[1], (顶点+索引[i+1])->位置[2]);
NSLog(@"%f %f %f", (顶点+索引[i+2])->位置[0], (顶点+索引[i+2])->位置[1], (顶点+索引[i+2])->位置[2]);
NSLog(@"%hu", **(wingedEdges[i].indexPointer));
}
尝试查看指针和结构的其他一些问题,但我没有找到任何东西。我在上次 NSLog 调用时收到错误。 NSLog 中使用索引和顶点调用的所有内容都是正确的,因此看起来可能是一个简单的语法错误或指针问题。另外,我如何增加indexPointer指向的指针?由于indexPointer指向一个indices指针,那么我想通过indexPointer访问indices+1和indices+2。
最佳答案
(&indices+i) 不指向您分配的任何内存。
有效的方法是将indexPointer和顶点更改为单个指针,然后
wingedEdges[i].indexPointer = &indices[i];
wingedEdges[i].vertex = &vertices[indices[i]];
那么*(wingedEdges[i].indexPointer)与indices[i]相同并且 wingedEdges[i].vertex->Position[0] 与 vertices[indices[i]].Position[0] 相同。但是,您将无法获得所需的自动更新(有关更多详细信息,请参阅我的评论)。我推荐一个简单的内联函数:
inline *Vertex vertex(WingedEdge* e)
{
return &vertices[*(e->indexPointer)];
}
关于objective-c - 指向 struct C/ObjC 中指针的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8427466/