array of structs pointers

Posted By: enrike

array of structs pointers - 12/09/09 08:54

hi

i want to create a global array of structs pointers to access later from a function but i am getting an error. i am not sure what i am doing wrong.

//////////////////////////
typedef struct {
int x;
int y;
int z;
int tilt;
int pan;
int roll;
} POS;

POS* cam0 = {
x = 61;
y = 8;
z = 20;
tilt = 0;
pan = 357;
roll = 0;
}

POS* cam1 = {
x = 261;
y = 23;
z = 0;
tilt = 110;
pan = 7;
roll = 22;
}

POS* cameras[2];
cameras[0] = cam0;
cameras[1] = cam1;


function loopPos()
{
for (i = 0; i < 2; i++) // repeat few times
{
cameras[i].x = 333; //ERROR COMES HERE
}

}
//////////////////////////////////

first i thought i should be able to do
POS* cameras[2] = { cam0, cam1 }
but it does not like it, then I am not sure why accessing the items in the array from the function is causing the crash. Actually doing the same ( cameras[0].x = 555; ) just after decalaring the array seems to work fine. Maybe it is a problem of scope? I thought that array should be global. I am calling the funcion from another file, but this one includes the one that defines the function, structs and the array...

thanks

enrike

Posted By: FoxHound

Re: array of structs pointers - 12/09/09 19:05

As long as the array is declared before it is called it doesn't matter what file you put it in. It will give you an "unknonw" type error if that was the case.

I haven't used cameras like this to much but don't they have "pos_x" instead of "x"? I could be wrong on that.
Posted By: Lukas

Re: array of structs pointers - 12/09/09 19:21

Try this:
POS* pos_temp;
...
pos_temp = cameras[i];
pos_temp.x = 333; //ERROR DOES NOT COME HERE ANYMORE (?)


EDIT: Uh, after looking at your code again I realised that you just create pointers and don't create the actual structs. You can do that with malloc:
cameras[i] = malloc(sizeof(POS));
Posted By: Bunsen

Re: array of structs pointers - 12/10/09 09:18

The Lite-C compiler is not able to use referencing/dereferencing outside of functions. You must simply move the 2 lines:

Code:
cameras[0] = cam0;	// equal to: *(camera+0) = cam0;
cameras[1] = cam1;	// equal to: *(camera+1) = cam1;



into a function. If you don't put it into a function the assignments are ignored
and both pointers cameras[0] and cameras[1] are NULL.


This will crash:

Code:
cameras[0] = cam0; // not working, cameras[0] will be NULL
cameras[1] = cam1; // same as above

function loopPos()
{
   for (i = 0; i < 2; i++) // repeat few times
   {
      cameras[i].x = 333; // as cameras[i] points to 0, it will cause a crash
   }
}




This will work:

Code:
function loopPos()
{
   cameras[0] = cam0;
   cameras[1] = cam1;
   for (i = 0; i < 2; i++) // repeat few times
   {
      cameras[i].x = 333;
   }
}


Posted By: enrike

Re: array of structs pointers - 12/10/09 09:28

hey! that was it. I put the asignment into the funtion and it works fine. good to know... i think this very same problem has been giving me some headaches somewhere else.
THANKS!!!
© 2024 lite-C Forums