My problem is the following – I have this lines of code:
// allocate a new vector
Vector3I *theVector = (Vector3I*)calloc(1,sizeof(Vector3I));
// write face's points to this vector
*theVector[0] = a3dsFace->points[0];
*theVector[1] = a3dsFace->points[1];
*theVector[2] = a3dsFace->points[2];
In the points[] array are the values {0,1,2}. When I write them to the Vector3I at which I am pointing to I get {0,0,0}. Do you have any suggestions about what I am doing wrong?
EDIT: Some more details:
This is from lib3ds at: http://code.google.com/p/lib3ds/
struct Lib3dsFace {
Lib3dsUserData user; /*! Arbitrary user data */
char material[64]; /*! Material name */
Lib3dsWord points[3]; /*! Indices into mesh points list */
Lib3dsWord flags; /*! See Lib3dsFaceFlag, below */
Lib3dsDword smoothing; /*! Bitmask; each bit identifies a group */
Lib3dsVector normal;
};
The a3dsFace is a Lib3dsFace struct.
And the points array is from this type:
typedef unsigned __int16 Lib3dsWord
And my Pointer:
Vector3I* theVector
is
typedef int Vector3I[3];
I hope this will bring some light to the problem.
With kind regards.
The below code does work and is a test of your code snippet. If something isn’t working, it could be a good idea to create a test like this, with hard-coded values for
*a3dsFace, in order to narrow down your problem.Output:
I put the addresses there so that you can see
(*theVector)[0]is a valid way of accessing the first element of your dynamically allocatedVector3I.Perhaps you haven’t set
a3dsFace->pointsproperly and that’s why{0, 0, 0}is being copied. Note also that you have each element of aVector3Ias typeint, and each point is of typeuint16_t. You also don’t need to usecallocto zero the allocated memory, since you’re immediately after assigning values to them; you could just usemalloc.Bottom line is you still haven’t provided enough code to find your exact problem, and you should add code to debug your code inside your code.
EDIT: I accidentally had
*theVector[0]which should have been and is now(*theVector)[0], since[]has higher precedence than*. Otherwise it’d cause undefined behaviour due to the fact you’re going past the bounds of the array, my bad. I don’t know how I forgot that when it was one of the main reasons I was going to post an answer before you made your edit. It worked, but if you ran it through a program like valgrind it’d tell you that something wasn’t quite right (even if it may have ran as expected).As you can see by the addresses outputted above, there’s quite a difference. For example having
*theVector[1], which because of operator precedence is the same as*(theVector[1]), will mean that it’ll increment the address pointed to bytheVectorby3 * sizeof(int)bytes (akasizeof(Vector3I)), instead of just1 * sizeof(int)in the (correct) case of(*theVector)[1]).