tags:

views:

139

answers:

2

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

+3  A: 

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.

VJo
Please format your code by putting four spaces at the beginning of each line or by using the "code" button in the toolbar. (I've gone in and done this for you here.)
Martin B
thanks. I just did it
VJo
A: 

So I try to explain my problem. So I have a function like that:

PointsPolygons(RtInt npolys, RtInt nverts[], RtInt verts[], RtInt n, char* tokens[], void* parms[])

And I'm trying to save all the arguments of this function in a class. So in n I have the number of tring in tokens[], and in parms I have alla the datas. Some datas are a array of points the point are declared like this:

typedef float RtPoint[3]

this is a snipset of my code:

struct RtVec
{
   float data[3];
};

vector<RtVec> m_pointV;

typdef void* RtPointer;
//Adress of the point arrays 
RtPointer ptr = parms[i];

for(int j=0;j<arraylen;++j)
{
    RtFloat* tmp = ((RtPoint*)(ptr))[j];
    RtVec m_Point;
    m_Point.val[0] = tmp[0]; m_Point.val[1] = tmp[1]; m_Point.val[2] = tmp[2];
    m_pointV.push_back(m_Point);  
 }
 //now I should save the adress of each members of in a m_pointV in a void* structure
 //to have samething like that (RtPointer)(new RtPoint*[arrlen]) and I have some problem to do that
 void m_params[i] = ;

thank you Alan

iaiotom
Please edit your question. Do not post comments or an addendum as answers.
Adam Robinson