编辑:好的,因为我之前太含糊了,这里是 SSCCE:
typedef float vector[3]
int mainLoaderFunc() {
char* memory = NULL;
size_t size = loadFile(fileName, &memory); // load model file into memory, this works, tested and true
// create vector arrays
vector *vertexArray = NULL;
vector *normalArray = NULL;
vector *textureArray = NULL;
loadArrays(size, memory, &vertexArray, &normalArray, &textureArray);
// do other stuff with arrays
}
void loadArrays(size_t size, char *memory, vector **vertexArray, vector **normalArray, vector **textureArray) {
int numVerts = 0;
int numNormals = 0;
int numTextures = 0;
char* p = memory; // pointer to start of memory
char* e = memory + size; // pointer to end of memory
// count verts, normals, textures for memory allocation
while (p != e) {
if (memcmp(p, "vn", 2) == 0) {
numNormals++;
} else if (memcmp(p, "vt", 2) == 0) {
numTextures++;
} else if (memcmp(p, "v", 1) == 0) {
numVerts++;
}
while (*p++ != (char) 0x0A);
}
// allocate memory for vector arrays
*vertexArray = new vector[numVerts];
*normalArray = new vector[numNormals];
*textureArray = new vector[numTextures];
int vertexIndex = 0;
int normalIndex = 0;
int textureIndex = 0; //*** IF BREAK POINT HERE: NO EXCEPTION
// load data from memory into arrays
while (p != e) {
if (memcmp(p, "vn", 2) == 0) {
sscanf(p, "vn %f %f %f", normalArray[normalIndex][0], normalArray[normalIndex][1], normalArray[normalIndex][2]);
normalIndex++;
} else if (memcmp(p, "vt", 2) == 0) {
sscanf(p, "vt %f %f", textureArray[textureIndex][0], textureArray[textureIndex][1]);
textureIndex++;
} else if (memcmp(p, "v", 1) == 0) {
sscanf(p, "v %f %f %f", vertexArray[vertexIndex][0], vertexArray[vertexIndex][1], vertexArray[vertexIndex][2]);
vertexIndex++;
}
while (*p++ != (char) 0x0A);
}
}
一旦代码命中 sscanf 部分,我就会得到异常,我尝试将 & 和 * 放在数组前面,但无论哪种方式我都会得到异常。
最佳答案
我猜你必须将地址传递给 sscanf
:
sscanf(myMemChunk, "%f %f %f", &myVector[i][0], &myVector[i][1], &myVector[i][2]);
关于c++ - 用sscanf使用指向float[3]的指针,得到异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8587802/