iaiotom
iaiotom

Reputation: 11

c++ and multi pointers

I have a simple problem with pointers. Here is my code:

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

So I just want put in p[0], p[1] and p[2] the address of c[0], c[1] and c[2]. My code is wrong but I didn't find a solution.


Sorry my fault this code works :)

  typedef float RtPoint[3]; 
  RtPoint** b = new RtPoint*[4];
  b[0] = (RtPoint*)new RtPoint;
  RtPoint c;
  b[0] = &c;

Edit: yes I've seen my error

Upvotes: 0

Views: 370

Answers (1)

BЈовић
BЈовић

Reputation: 64203

If you are doing c++, forget about raw pointers, and use the std::vector.

But if you really insists :

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

By the way, you didn't initialize any variable.

Upvotes: 3

Related Questions