...其中讨论了(A)指针数组和(B)指向数组的指针.这让我再次感到困惑,因为他们似乎都工作类似.我在一个单独的位置定义我的指针的事实,这似乎是我感到困惑的地方. 我是否纠正为D3DXVECTOR3对象的指针数组? 要完成-如果可变行包含有关一个线段的信息,我将如何创建线段数组?我目前有以下内容-//HEADER FILEint numberOfLineSegments;D3DXVECTOR3** lineCollection; //array of pointers - each of which //points to an array of pointers?//CPP FILEnumberOfLineSegments = 8; //constructorfor(i = 0; i < numberOfLineSegments; i++) //initialization{ //and allocation CORRECT? lineCollection[i] = new D3DXVECTOR*[numPoints]; //of memory for Y/N} //lineCollectionVOID createLineSegments(startPoint, endPoint) //could return array instead{//pseudo to generate one line segmentwhile != numberOfLinePointsline[sentinel++] = interpolate(startPoint, endPoint, time_T)//pseudo to generate array of line segmentswhile != numberOfLineslineCollection[sentinel++] = line}非常感谢您的帮助.解决方案 int numberOfLinePoints;D3DXVECTOR3* line; //confused as to what it is//both initialized in constructornumberOfLinePoints = 25;line = new D3DXVECTOR3[numPoints]; //array of pointers? line是D3DXVECTOR3的数组.但是,如果D3DVECTOR3本身是指针,它将是一个指针数组.由于我不太了解C ++ D3D标头,因此不确定.D3DXVECTOR3** lineCollection;是一个指针数组,每个指针很可能是指向一行的指针(即,D3DXVECTOR3的数组).您有两个选择.从内存的角度来看,最好的方法是将lineCollection中的每个条目设置为仅指向相应的行.如果您知道行不会更改(也不会被释放),或者如果行确实更改了,您希望更改立即反映在集合中,则这是安全的.另一种选择是为lineCollection中的每个条目创建一个新数组,并将每个line中的点复制到该新数组中.没有正确的答案,这取决于您想要的功能.I notice this has caused confusion for several people, but after reading a couple of posts on here and the cplusplus tutorial my brain is still scrambled.Suppose I have the following variables in a header file -int numberOfLinePoints;D3DXVECTOR3* line; //confused as to what it isThen in the implementation C++ file I initialize them as follows -//both initialized in constructornumberOfLinePoints = 25;line = new D3DXVECTOR3[numPoints]; //array of pointers?What does my line variable now represent?As far as I can tell from reading links on stackoverflow it should represent an array of pointers. I then read the following however...(1) Pointers for Beginners...where (A) arrays of pointers, and (B) pointers to arrays, are both discussed. This left me confused once again as they both seem to work similarly.The fact that I define my pointers in a seperate location to where I allocate (correct?) them seems to be where my confusion stems from. Am I correct that this is an array of pointers to D3DXVECTOR3 objects?To finish - if variable line holds information about one line segment, how would I create an array of line segments? I currently have the following -//HEADER FILEint numberOfLineSegments;D3DXVECTOR3** lineCollection; //array of pointers - each of which //points to an array of pointers?//CPP FILEnumberOfLineSegments = 8; //constructorfor(i = 0; i < numberOfLineSegments; i++) //initialization{ //and allocation CORRECT? lineCollection[i] = new D3DXVECTOR*[numPoints]; //of memory for Y/N} //lineCollectionVOID createLineSegments(startPoint, endPoint) //could return array instead{//pseudo to generate one line segmentwhile != numberOfLinePointsline[sentinel++] = interpolate(startPoint, endPoint, time_T)//pseudo to generate array of line segmentswhile != numberOfLineslineCollection[sentinel++] = line}Any help is much appreciated. 解决方案 int numberOfLinePoints;D3DXVECTOR3* line; //confused as to what it is//both initialized in constructornumberOfLinePoints = 25;line = new D3DXVECTOR3[numPoints]; //array of pointers?line is an array of D3DXVECTOR3. It would be an array of pointers if D3DVECTOR3 is itself a pointer, however. Since I don't know the C++ D3D headers very well, I'm not sure.D3DXVECTOR3** lineCollection;Is an array of pointers, each pointer likely being a pointer to a line (that is, an array of D3DXVECTOR3).You have two options. Memorywise, the best would be to set each entry in lineCollection to just point to the corresponding line. This is safe if you either know the lines aren't going to change (and aren't going to be freed), or if they do change you want the changes to be reflected immedaitely inside your collection.The other option would be to create a new array for each entry in lineCollection, and copy the points from each line into this new array.There is no correct answer, it depends on the functionality you want. 这篇关于C ++-指向数组的指针-指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!