我对指针有一个简单的问题。这是我的代码:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
typedef float RtPoint[3];
RtPoint** b = new RtPoint*[4];
b[0] = (RtPoint*)new RtPoint;
RtPoint* p = b[0];
RtPoint c;
(*p)[0] = &(c[0]);
(*p)[1] = &(c[1]);
(*p)[2] = &(c[2]);
std::cout << p[1] << " " << &(c[0]) << std::endl;
delete[] b;
return 0;
}
所以我只想将
p[0]
,p[1]
和p[2]
的地址放在c[0]
,c[1]
和c[2]
中。我的代码是错误的,但没有找到解决方案。对不起我的错,此代码有效:)
typedef float RtPoint[3];
RtPoint** b = new RtPoint*[4];
b[0] = (RtPoint*)new RtPoint;
RtPoint c;
b[0] = &c;
编辑:是的,我已经看到我的错误
最佳答案
如果您使用的是c ++,请忽略原始指针,并使用std :: vector。
但是,如果您真的坚持:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
typedef float RtPoint[3];
RtPoint** b = new RtPoint*[4];
b[0] = (RtPoint*)new RtPoint;
RtPoint* p = b[0];
RtPoint c;
(*p)[0] = c[0];
(*p)[1] = c[1];
(*p)[2] = c[2];
std::cout << p[1] << " " << &(c[0]) << std::endl;
delete[] b;
}
顺便说一句,您没有初始化任何变量。