Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pointer trouble 1

Status
Not open for further replies.

Maurader

Technical User
May 8, 2002
59
CA
if:

typedef double p_3d[3]; and

struct triangle{
p_3d* points[3] // three points to p_3d
}

and I have a vector <p_3d> allPoints,

when I do triangle.points[0] = &(allPoints) I get

'&' requires l-value

what is the correct way to do this assignment?

thanks!
 
First of all, I don't think vector works very well with structures as it is designed to work with classes.

Second, are you using the type name for a variable dereferencing? You should have a variable of type struct triangle to work with, not with the TYPE itself...

That's just like writing:
Code:
typedef unsigned long ulong;

ulong = 7;

You should have something like:
Code:
struct triangle tr;
tr.points[0] = allPoints[0];
tr.points[1] = allPoints[1];
I haven't tried the assignments above, but in any case you won't be able to assign a vector typed variable to tr.points. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
instead of using a vector, what should I use?

crap, i copied that code entirely wrong...i actually have

triangle tri
tri.points[0] = &(allPoints[0]);

a little bit confused on what allPoints[0] returns...a struct? or an address?

thanks a lot!
 
Hi
actually your usage seems to smell of stress testing, rather than typical usage. But thats ok.

Using vector<T> where T is any type (such as your p_3d) is fine. T need not be an explicitly defined class. I have used vector<int> successfully many times.

One would typically use vector instead of x = new T[3] ;
if the size of the array is not known even during the course of the execution. I mean, the allPoints you created, i guess you intend to grow the number of elemnts in it, but how much you dont know. I guess you know that already.

Now remember that allPoints[0] is actually allPoints.opertor[] (0 ) i.e. there is a function call. And & (allPoints[](0) ) means
taking address of the return value of the function !

The problem is bit more than that in your case, since allPoints[0] returns double [3]. also you need a default copy contructor for the object p_3d. Whether vector<p_3d> will
create space for your array, or it will just hold a pointer i am not sure.
Perhaps if you could explain what your intended data structure is , then i could suggest something instead of making a running commentary of dos-donts of vectors.
ciao
 
p_3d is actually an API typedef, and it's used to hold XYZ coordinates of points in 3-space. mmm, my application (not stress test!) is that I have a solid, and have a triangulated mesh genereated, which means i've created a surface out of adjacent triangles that entirely covers my solid. my data structure Triangle is used to hold information about each triangle in the mesh. allPoints will hold a list of all vertices in this mesh. There will be another vector of Triangle objects, each having three pointers. each pointer will point to a vertex contained in allPoints.

that's pretty much what i'm doing...thanks for all help! and the explanation of vector is very helpful!
 
here's my reasoning:

allPoints[0] returns double[3]
so &(allPoitns[0]) returns a pointer to double[3]?
I also tried without the &quot;&&quot; since i'm thinking double[3] is actually an address of the first element of the array...
but it's giving me an error saying that there is a difference between const double[3] and double[3] when i say
p_3d* point;
point = allPoints[0]
when is the default copy constructor used?
 
ok, now that you explain the application, the it does not look like a stress test anymore.

so
typedef double p_3d[3];
is fixed.
And you dont want duplications so
you want AllPoints to keep all-the-points
while the triangles are formed using pointers to these points. Am i right ?

how about this:
struct Traingle{ double *vert[3]; };
main(){

vector<p_3d*> AllPoints;
p_3d point; point[0] = 3; .....
p_3d* ptr2pt = &point;
allPoints.push_back( ptr2pt) ;
struct Traingle t;
t.vert[0] = point;
t.vert[0][0] = 6;
}

the problem with the initial solution now i tihnk , was that the moment you define an array
double p_3d[3];
p_3d is a const pointer to double.

in the above solution, my intnetion was to store pointer to the point, bcos then the default copy constructor (called when parameters are passed or if allPoints = ... is used) works fine with a pointer, but not when an the intention is to copy an entire array.
The vector works on the assumption that it keeps a copy of the data that it later gives bk, unlike our C array, where a means a fixed location which can be tampered with and whose address can be taken
 
<ignore previous cos of typos>
ok, now that you explain the application, it does not look like a stress test anymore.

so
typedef double p_3d[3];
is fixed.
And you dont want duplications so
you want AllPoints to keep all-the-points
while the triangles are formed using pointers to these points. Am i right ?

how about this:
struct Traingle{ double *vert[3]; };
main(){

vector<p_3d*> AllPoints;

p_3d point; point[0] = 3; .....

p_3d* ptr2pt = &point;

allPoints.push_back( ptr2pt) ;

struct Traingle t;

t.vert[0] = point;
t.vert[0][0] = 6;
}

the problem with the initial solution now i think , was that the moment you define an array
double p_3d[3];
p_3d is a const pointer to double.

in the above solution, my intnetion was to store pointer to the point in AllPoint, bcos then the default copy constructor (called when parameters are passed or if allPoints = ... is used) works fine with a pointer, but not when the intention is to copy an entire array.
The vector works on the assumption that it keeps a copy of the data that it later gives bk, unlike our C array, where a(i) means a fixed location which can be tampered with and whose address can be taken
 
ahh, things are starting to clear up...thanks a lot for you detailed explanations!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top