objective-c - 指向 struct C/ObjC 中指针的指针

标签 objective-c c pointers struct syntax-error

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/

相关文章:

ios - 如何在iphone中保存缩放后的图片

iphone - 如何在 _NSLockError() 上设置断点

c++ - 什么定义了递归函数?

c - 为什么不接受()阻止?

c - 如何为结构、结构内的结构数组正确分配内存,并将该数组作为参数传递

ios - UITableView 中的二进制数据导致高 CPU 负载和卡住

JAVA Web Service with SOAP Client Objective C---响应问题

c - 如何在 C 中打印函数指针的值?

C++:为什么 iter++->empty() 合法?

c++ - 简单的功能大小;了解指针与指针的区别