Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by EternallyCurious. 04/18/24 10:45
StartWeek not working as it should
by Zheka. 04/18/24 10:11
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 559 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, 11honza11, ccorrea, sakolin, rajesh7827
19046 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Point to a Struct #310732
02/16/10 18:41
02/16/10 18:41
Joined: Jul 2009
Posts: 36
S
SomebodyNew Offline OP
Newbie
SomebodyNew  Offline OP
Newbie
S

Joined: Jul 2009
Posts: 36
Hello there,
I want to create a struct that can point to a number of other structs (basically a tree consisting of different vertexes).
This is what I tried:

Code:
// define a struct-type named "Body_Struct"
typedef struct { 
  	int Volume;
  	int Mass;
} Body_Struct;

// Initializes a Body_Struct and sets the pointer Ptr_Slot1 to it
Body_Struct* Ptr_Slot1 = {Volume = 10; Mass = 10;}
// Initializes a Body_Struct and sets the pointer Ptr_Slot2 to it
Body_Struct* Ptr_Slot2 = {Volume = 20; Mass = 20;}

// define a second struct-type named "Geometry_Struct"
typedef struct { 
  	int Price;
  	struct Body_Struct* Pointer1;
  	struct Body_Struct* Pointer2;
} Geometry_Struct;

Geometry_Struct Geometry1;
Geometry1.Price = 99.95;
Geometry1.Pointer1 = Ptr_Slot1;
Geometry1.Pointer2 = Ptr_Slot2;



Every time I try to write/read a value via Geometry_Struct.Pointer1.Mass the engine returns a pointer-error and crashes.
So apparently the pointer are not set up correctly.

I don't understand what I did wrong though.
(already checked the help/manual/forum search).



The way I understand it, this line:
Body_Struct* Ptr_Slot2 = {Volume = 20; Mass = 20;}
a) Creates a new struct of type Body_Struct
b) Assigns a pointer named Ptr_Slot2 to this struct-instance
c) Accesses the newly created struct via the pointer
d) and assigns Ptr_Slot2.Volume = 20 and Ptr_Slot2.Mass = 20

I get the same error when using an array of pointers in the Geometry struct where each pointer points to a different struct and then using:
Geometry1.Array[2].Mass


Any help is greatly appreciated.
Have a nice evening.

Re: Point to a Struct [Re: SomebodyNew] #310742
02/16/10 19:11
02/16/10 19:11
Joined: Nov 2007
Posts: 1,143
United Kingdom
DJBMASTER Offline
Serious User
DJBMASTER  Offline
Serious User

Joined: Nov 2007
Posts: 1,143
United Kingdom
I had no problem accessing it, I just remove the 'struct' keywords from the 'Geometry_Struct' like so...
Code:
typedef struct { 
  	int Price;
  	Body_Struct* Pointer1;
  	Body_Struct* Pointer2;
} Geometry_Struct;



I'm not too sure why the manual uses the 'struct' keyword when defining a member also.

Re: Point to a Struct [Re: DJBMASTER] #310752
02/16/10 19:50
02/16/10 19:50
Joined: Jul 2009
Posts: 36
S
SomebodyNew Offline OP
Newbie
SomebodyNew  Offline OP
Newbie
S

Joined: Jul 2009
Posts: 36
Thanks DJBMASTER,
after removing the "struct" part, this is the code I use at the moment.
It stil crashes though.
Code:
#include <acknex.h>
#include <default.c>

typedef struct { 
  	int Volume;
  	int Mass;
} Body_Struct;

Body_Struct* Ptr_Slot1 = {Volume = 10; Mass = 10;}
Body_Struct* Ptr_Slot2 = {Volume = 20; Mass = 20;}

typedef struct { 
  	int Price;
  	Body_Struct* Pointer1;
  	Body_Struct* Pointer2;
} Geometry_Struct;

Geometry_Struct Geometry1;
Geometry1.Price = 99.95;
Geometry1.Pointer1 = Ptr_Slot1;
Geometry1.Pointer2 = Ptr_Slot2;

function main();

function main()
{
	camera.z = 1000;	
	if (Geometry1.Pointer1.Mass == 10) {camera.z = 20;}
}



The problem then propably lies with the if statement.
I believe lite-C does not have a special command to differentiate between a pointer and the value the pointer points to.

Re: Point to a Struct [Re: SomebodyNew] #310795
02/17/10 00:51
02/17/10 00:51
Joined: Jul 2008
Posts: 1,178
England
M
MrGuest Offline
Serious User
MrGuest  Offline
Serious User
M

Joined: Jul 2008
Posts: 1,178
England
Code:
#include <acknex.h>
#include <default.c>

typedef struct BODY_STRUCT {
	
	int volume;
	int mass;
	
} BODY_STRUCT;

BODY_STRUCT* ptr_slot1 = {volume = 10; mass = 15;}
BODY_STRUCT* ptr_slot2 = {volume = 20; mass = 25;}

typedef struct GEOMETRY_STRUCT {
	
	var price;
	BODY_STRUCT* pointer1;
	BODY_STRUCT* pointer2;
} GEOMETRY_STRUCT;

GEOMETRY_STRUCT geometry1;


void main(){
	
	//Geometry_Struct Geometry1;
	geometry1.price = 99.95;
	geometry1.pointer1 = ptr_slot1;
	geometry1.pointer2 = ptr_slot2;
	
	wait(1);
	
	camera.z = 1000;
	
	if(geometry1.pointer1.volume == 10){
		camera.z = 20;
		beep();
	}
}

will work for you, sorry about changing the cases,
also i changed price to a var, as you'll want it to support decimals (99.95)
you only need

Code:
struct BODY_STRUCT* Pointer1;

if you were using the same struct within itself

hope this helps

Re: Point to a Struct [Re: MrGuest] #310802
02/17/10 03:05
02/17/10 03:05
Joined: Jul 2009
Posts: 36
S
SomebodyNew Offline OP
Newbie
SomebodyNew  Offline OP
Newbie
S

Joined: Jul 2009
Posts: 36
Thank you so much!
The code works perfectly fine now.

Looking at your code I think the uppercase-style is way more readable. So thanks for that too =)

I just figured out how to use the above info to create an array that includes several pointers which I find incredibly helpfull when making structs:
BODY_STRUCT* Array[n];
(just in case somebody wonders)

Ok, thanks for all your help.
Problem solved,
good night =)


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