Derefferncing a pointer to a struct

Started by
4 comments, last by DarkMatter2008 15 years, 2 months ago
Hello All, Is derefferncing a pointer to a struct possible? My compiler doesn't seem to think so. Vector temp1 = *ptr->v3; //ptr is a pointer to a polygon c:\tutorial\gouraud\gp\gp\main.cpp(492) : error C2440: 'initializing' : cannot convert from 'Vector *' to 'Vector' [\code] Thanks in advance.
Advertisement
You'll probably need some paren orientation to get the order of operations right.
Quote:Original post by DarkMatter2008
Vector temp1 = *ptr->v3; //ptr is a pointer to a polygon


Why are you dereferencing (*) the pointer? Assuming:

struct Poly{    Vector v;};void f(Poly *ptr){    Vector temp1=ptr->v;}


would be fine. ptr->v is like a shortcut to (*ptr).v so no dereferencing is necessary.

You would only need the asterisk if you were dealing with a pointer-to-pointer-to Poly:

void f(Poly **ptr){    Vector temp1=(*ptr)->v; // note parens as per Telastyn's comment above}
This is more like what it is;

struct Vector{//Vector information here}struct Poly{    Vector *v;};void f(Poly *ptr){    Vector temp1=ptr->v; // Needs to be derefferenced so that the Vector can be copied.}


I'm trying to dereffernce the struct pointer in order to copy the struct.
(*(ptr->v)); (iirc)
Quote:Original post by Telastyn
(*(ptr->v)); (iirc)


hmmm, thouse extra set of brackets seem to have done the trick.

I tried this before
*(ptr->v)

with out the outer ones but it didn't work.

anyways, thank you very much Telastyn :)

This topic is closed to new replies.

Advertisement