|
Little problem with pointer
#313792
03/03/10 17:28
03/03/10 17:28
|
Joined: Jun 2004
Posts: 655 to your left
BoH_Havoc
OP
User
|
OP
User
Joined: Jun 2004
Posts: 655
to your left
|
I have a small(?) problem here:
typedef struct
{
VECTOR3D n;
float d;
} Plane;
typedef struct
{
Plane* plane;
int size;
} VecPlane;
void planesSetSize(VecPlane* p, const int csize)
{
if (p != NULL)
{
if (csize != p->size)
{
*p->plane = (Plane*)realloc(p->plane, csize*sizeof(Plane));
*p->size = csize;
}
}
}
when compiling i get "can not convert 'POINTER' to 'struct Plane'" for *p->plane = (Plane*)realloc(p->plane, csize*sizeof(Plane)); and for *p->size = csize; Tried a lot of combinations with () and * but so far none worked Any help appreciated
|
|
|
Re: Little problem with pointer
[Re: BoH_Havoc]
#313795
03/03/10 17:36
03/03/10 17:36
|
Joined: Nov 2007
Posts: 1,143 United Kingdom
DJBMASTER
Serious User
|
Serious User
Joined: Nov 2007
Posts: 1,143
United Kingdom
|
*p->plane = (Plane*)realloc(p->plane, csize*sizeof(Plane));
*p->size = csize;
I think this is your problem. You dereference the pointer, and then use the 'member-by-pointer' operator, when you really want a 'member-by-variable'. So just remove the * ...
p->plane = (Plane*)realloc(p->plane, csize*sizeof(Plane));
p->size = csize;
or
(*p).plane = (Plane*)realloc(p->plane, csize*sizeof(Plane));
(*p).size = csize;
Last edited by DJBMASTER; 03/03/10 17:38.
|
|
|
Re: Little problem with pointer
[Re: DJBMASTER]
#313798
03/03/10 17:43
03/03/10 17:43
|
Joined: Jun 2004
Posts: 655 to your left
BoH_Havoc
OP
User
|
OP
User
Joined: Jun 2004
Posts: 655
to your left
|
HMmm...but if i remove the *, will the original p be altered ? VecPlane orgP; planesSetSize(orgP, 10); is orgP = p from the function now? *scratches his head* thanks for the quick reply
|
|
|
Re: Little problem with pointer
[Re: BoH_Havoc]
#313802
03/03/10 17:57
03/03/10 17:57
|
Joined: Nov 2007
Posts: 1,143 United Kingdom
DJBMASTER
Serious User
|
Serious User
Joined: Nov 2007
Posts: 1,143
United Kingdom
|
In the snippet you posted, no, because it wants a pointer to a struct, not the struct itself, so it probably wont even compile.
Your original code is fine it's just you've got a syntax error. You can't use *a->b because -> gets the member b from the pointer a, and so by derefencing it, you are turning a into the variable, where -> fails.
All this pointer stuff gets confusing after a while, but I think your code is correct except for those 2 lines I posted earlier, where you are mixing * with ->.
Last edited by DJBMASTER; 03/03/10 17:59.
|
|
|
|