C – 指向数组的指针 – 指针数组
我注意到这引起了一些人的困惑,但在阅读了这里的几篇帖子和cplusplus教程后,我的大脑仍然被打乱.
假设我在头文件中有以下变量 – int numberOfLinePoints; D3DXVECTOR3* line; //confused as to what it is 然后在实现C文件中我将它们初始化如下 – //both initialized in constructor numberOfLinePoints = 25; line = new D3DXVECTOR3[numPoints]; //array of pointers? 我的行变量现在代表什么? 据我所知,通过读取stackoverflow上的链接,它应该代表一个指针数组.然后,我读了以下内容…… (1)Pointers for Beginners …其中(A)指针数组和(B)指向数组的指针都在讨论中.这让我感到困惑,因为它们似乎都有类似的作用. 事实上,我在一个单独的位置定义我的指针到我分配的位置(正确吗?)它们似乎是我的困惑所源自的地方.我是否正确,这是D3DXVECTOR3对象的指针数组? 完成 – 如果变量行保存有关一个线段的信息,我将如何创建一个线段数组?我目前有以下 – //HEADER FILE int numberOfLineSegments; D3DXVECTOR3** lineCollection; //array of pointers - each of which //points to an array of pointers? //CPP FILE numberOfLineSegments = 8; //constructor for(i = 0; i < numberOfLineSegments; i++) //initialization { //and allocation CORRECT? lineCollection[i] = new D3DXVECTOR*[numPoints]; //of memory for Y/N } //lineCollection VOID createLineSegments(startPoint,endPoint) //could return array instead { //pseudo to generate one line segment while != numberOfLinePoints line[sentinel++] = interpolate(startPoint,endPoint,time_T) //pseudo to generate array of line segments while != numberOfLines lineCollection[sentinel++] = line } 任何帮助深表感谢. 解决方法int numberOfLinePoints; D3DXVECTOR3* line; //confused as to what it is //both initialized in constructor numberOfLinePoints = 25; line = new D3DXVECTOR3[numPoints]; //array of pointers? line是D3DXVECTOR3的数组.但是,如果D3DVECTOR3本身就是指针,它将是一个指针数组.由于我不太了解C D3D标题,我不确定. D3DXVECTOR3** lineCollection; 是一个指针数组,每个指针可能是一条指向一条线的指针(即D3DXVECTOR3的数组). 你有两个选择.在Memorywise中,最好的方法是将lineCollection中的每个条目设置为仅指向相应的行.如果您知道线路不会更改(并且不会被释放),或者如果它们确实发生更改,您希望更改能够立即反映在您的集合中,这是安全的. 另一种选择是为lineCollection中的每个条目创建一个新数组,并将每行中的点复制到这个新数组中. 没有正确答案,这取决于您想要的功能. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |