2011-12-08 66 views
0

指向在一個結構的C指針/ ObjC

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訪問index + 1和index + 2。

+1

其中是變量索引初始化代碼? – changx

+0

我添加了它,不能把所有東西都放在這裏,否則它會很大,但是所有與wingedEdges相關的代碼應該在這裏。 – MCH

+0

如果您想知道爲什麼我使用指向指針的指針,那麼只是爲了防止頂點或索引中的值發生更改,這樣我就不必再次更改wingedEdge中的值。 – MCH

回答

0

(& indices + i)不指向您分配的任何內存。

什麼將工作是將indexPointer和頂點更改爲單個指針和然後

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

然後*(wingedEdges [I] .indexPointer)是一樣的指數[i]和 wingedEdges [I] .vertex-> Position [0]與頂點[indices [i]]相同。Position [0]。但是,您將無法獲得所需的自動更新(請參閱我的評論以獲取更多詳細信息)。我推薦一個簡單的內聯函數:

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

編譯器不喜歡:地址表達式必須是Ivalue或函數指示符(Xcode) – MCH

+0

但我認爲你是在正確的軌道上,因爲如果我只是去(&指數)然後沒有錯誤,但是,這不是我想要的價值,需要抵消它。 – MCH

+0

@MCH:'&indices [i]'怎麼樣? –

相關問題