c ++和多指针

时间:2010-10-15 13:34:33

标签: c++ pointers

指针有一个简单的问题。这是我的代码:

#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;

编辑:是的我看到了我的错误

1 个答案:

答案 0 :(得分:3)

如果您正在使用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;
}

顺便说一下,你没有初始化任何变量。