I have a unit test program i wrote to catch errors in the functions of an open source project before the initial compilation. All of the functions i need to test return a pointer to a struct(in this case bn_poly) while my variable is NOT a pointer. I know there must be an easier solution to this problem but after a bit of googling i came up with nothing, here is what i’m currently doing
/* only relevant part of code */
struct *bn_poly poly_summ;
unsigned i
poly_summ = (struct bn_poly*)bu_malloc(sizeof(struct bn_poly),"sum");
/* test multiplication and begin checking data */
*poly_summ = bn_poly_mul(&poly_summ,poly_eqn,poly_mul);
/* bn_poly returns a pointer to bn_poly, so i have to declare
* a temporary pointer to find the value. Is this the only way
* or is there a type cast method i am missing?
*/
what i would like to be able to do:
struct bn_poly poly_summ;
poly_summ = (somethingToConvertReturnVal)bn_poly_mul(&poly_summ,poly_eqn,poly_mul);
/* no memory allocation needed! */
I’m a little bit confused regarding what your question is But here’s what i know about “casting” pointers.
the
&symbol is the reference symbol, so&xgives you the address of x, or a pointer which points to xthe
*symbol is the deference symbol, so supposexis a pointer,*xgives you the valuexpoints toin your case, i think you want
as an additional note, by the looks of it,
bn_poly_mullooks like it already allocates your struct and puts it on the heap. You don’t even need to allocate it, in-fact doing so would result in a memory leakAlso, the fact that the first parameter of
bn_poly_mulis&poly_summis a little bit off-putting to me, so I’d have to see the function to know for sure