Gamestudio Links
Zorro Links
Newest Posts
Free Live Data for Zorro with Paper Trading?
by dr_panther. 05/18/24 11:01
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (7th_zorro, dr_panther), 724 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
array of structs pointers #301254
12/09/09 08:54
12/09/09 08:54
Joined: Jul 2009
Posts: 40
E
enrike Offline OP
Newbie
enrike  Offline OP
Newbie
E

Joined: Jul 2009
Posts: 40
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


Last edited by enrike; 12/09/09 09:00.
Re: array of structs pointers [Re: enrike] #301343
12/09/09 19:05
12/09/09 19:05
Joined: Jun 2004
Posts: 2,234
Wisconsin USA
FoxHound Offline
Expert
FoxHound  Offline
Expert

Joined: Jun 2004
Posts: 2,234
Wisconsin USA
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.


---------------------
There is no signature here.


QUIT LOOKING FOR ONE!
Re: array of structs pointers [Re: FoxHound] #301344
12/09/09 19:21
12/09/09 19:21
Joined: May 2007
Posts: 2,043
Germany
Lukas Offline

Programmer
Lukas  Offline

Programmer

Joined: May 2007
Posts: 2,043
Germany
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));

Last edited by Lukas; 12/09/09 19:24.
Re: array of structs pointers [Re: Lukas] #301401
12/10/09 09:18
12/10/09 09:18
Joined: Apr 2009
Posts: 33
Germany
B
Bunsen Offline
Newbie
Bunsen  Offline
Newbie
B

Joined: Apr 2009
Posts: 33
Germany
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;
   }
}



Re: array of structs pointers [Re: Bunsen] #301402
12/10/09 09:28
12/10/09 09:28
Joined: Jul 2009
Posts: 40
E
enrike Offline OP
Newbie
enrike  Offline OP
Newbie
E

Joined: Jul 2009
Posts: 40
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!!!


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1