Little problem with pointer

Posted By: BoH_Havoc

Little problem with pointer - 03/03/10 17:28

I have a small(?) problem here:

Code:
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
Quote:
"can not convert 'POINTER' to 'struct Plane'"

for *p->plane = (Plane*)realloc(p->plane, csize*sizeof(Plane));

and
Quote:
illegal indirection

for *p->size = csize;


Tried a lot of combinations with () and * but so far none worked wink

Any help appreciated laugh
Posted By: DJBMASTER

Re: Little problem with pointer - 03/03/10 17:36

Code:
*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 * ...
Code:
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;


Posted By: BoH_Havoc

Re: Little problem with pointer - 03/03/10 17:43

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 laugh


Posted By: DJBMASTER

Re: Little problem with pointer - 03/03/10 17:57

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 ->.
Posted By: Saturnus

Re: Little problem with pointer - 03/03/10 22:21

Yeah, DJBMASTER is right here: use the two code lines posted by him.

"HMmm...but if i remove the *, will the original p be altered ?"
Yes. In lite-C you can't pass structs by value, so p still points to your original struct.

"VecPlane orgP;
planesSetSize(orgP, 10);"

lite-C is supposed to differentiate between objects and pointers automatically (unless you set PRAGMA_POINTER). However, to be on the safe side you should use the address operator here: planesSetSize(&orgP, 10)
Posted By: BoH_Havoc

Re: Little problem with pointer - 03/04/10 21:42

Alrighty, thanks a bunch guys! laugh
© 2024 lite-C Forums