c++ - Get Pointer Value -
i new c++ pointers, , have issue getting value pointer.
i have pointer, verticesposbegin , points beginning of array used hold vertex positions. each vertex stored 3 component float vector (xyz).
i need obtain each vertex , access x,y,z values.
i did following way:
nxvec3* positions = (nxvec3*)data.verticesposbegin; for(int i=0;i<nrvertices;i++) { nxvec3* p1 = (nxvec3*)positions; printf("vertex coordinates x: %d, y: %d, z: %d\n", p1->x, p1->y, p1->z); positions++; }
(nxvec3 juat type defined physics engine use, structure of form (float x, float y, float z))
but not me values of coordinates, addresses, guess, since represent large numbers. appreciated.
according statement, p1->x
, p1->y
, p1->z
of type float
, correct? if so, passing incorrect format string printf. %d
flag integers. want %f
flag instead. huge numbers getting not addresses, rather float values, converted doubles, bit patterns interpreted integers, though technically undefined behavior.
http://en.cppreference.com/w/cpp/io/c/fprintf
if use cout instead, don't have worry things this, because type safe.
p.s.
stop casting. hide compile-time, , shift them run-time errors, worse.
Comments
Post a Comment