指向struct C / ObjC中指针的指针

时间:2011-12-08 07:12:52

标签: objective-c c pointers struct syntax-error

typedef struct {  
    float Position[3];  
    float Color[4];  
    float VertexNormal[3];  
} Vertex;  

typedef struct WingedEdge{  
    struct WingedEdge* sym;  
    struct WingedEdge* next;  
    struct WingedEdge* prev;  
    Vertex** vertex;  
    GLushort** indexPointer;  
} WingedEdge;  

Vertex* vertices;  
GLushort* indices;  
struct WingedEdge* wingedEdges;  
int numberOfVertices; //initialized elsewhere
int numberOfIndices; //initialized elsewhere,this is multiplied by three since I am not using a struct for the indices
vertices = (Vertex *) malloc(numberOfVertices * sizeof(Vertex));
indices = (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", *(indices+i),*(indices+i+1),indices[i+2]); 
    NSLog(@"%f %f %f", (vertices+indices[i])->Position[0], (vertices+indices[i])->Position[1], (vertices+indices[i])->Position[2]);
    NSLog(@"%f %f %f", (vertices+indices[i+1])->Position[0], (vertices+indices[i+1])->Position[1], (vertices+indices[i+1])->Position[2]);
    NSLog(@"%f %f %f", (vertices+indices[i+2])->Position[0], (vertices+indices[i+2])->Position[1], (vertices+indices[i+2])->Position[2]);

    NSLog(@"%hu", **(wingedEdges[i].indexPointer));  
}    

typedef struct { float Position[3]; float Color[4]; float VertexNormal[3]; } Vertex; typedef struct WingedEdge{ struct WingedEdge* sym; struct WingedEdge* next; struct WingedEdge* prev; Vertex** vertex; GLushort** indexPointer; } WingedEdge; Vertex* vertices; GLushort* indices; struct WingedEdge* wingedEdges; int numberOfVertices; //initialized elsewhere int numberOfIndices; //initialized elsewhere,this is multiplied by three since I am not using a struct for the indices vertices = (Vertex *) malloc(numberOfVertices * sizeof(Vertex)); indices = (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", *(indices+i),*(indices+i+1),indices[i+2]); NSLog(@"%f %f %f", (vertices+indices[i])->Position[0], (vertices+indices[i])->Position[1], (vertices+indices[i])->Position[2]); NSLog(@"%f %f %f", (vertices+indices[i+1])->Position[0], (vertices+indices[i+1])->Position[1], (vertices+indices[i+1])->Position[2]); NSLog(@"%f %f %f", (vertices+indices[i+2])->Position[0], (vertices+indices[i+2])->Position[1], (vertices+indices[i+2])->Position[2]); NSLog(@"%hu", **(wingedEdges[i].indexPointer)); }

尝试用指针和结构查看其他一些问题,但我没有找到任何东西。我在上一次NSLog调用时遇到错误。使用索引和顶点的NSLog调用中的所有内容都是正确的,因此看起来它可能是一个简单的语法错误或指针问题。另外,我如何增加indexPointer指向的指针?由于indexPointer指向索引指针,因此我想通过indexPointer访问索引+ 1和索引+2。

1 个答案:

答案 0 :(得分:0)

(&amp; indices + i)不指向您已分配的任何内存。

将indexPointer和顶点更改为单指针然后

wingedEdges[i].indexPointer = &indices[i];
wingedEdges[i].vertex = &vertices[indices[i]];

然后*(wingedEdges [i] .indexPointer)与indices [i]和 wingedEdges [i] .vertex-&gt; Position [0]与顶点[indices [i]]相同。位置[0]。但是,您将无法获得所需的自动更新(有关详细信息,请参阅我的注释)。我推荐一个简单的内联函数:

inline *Vertex vertex(WingedEdge* e) 
{
    return &vertices[*(e->indexPointer)];
}