Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
2 registered members (TipmyPip, 1 invisible), 18,758 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Lite-C structs problem #137996
06/25/07 16:12
06/25/07 16:12
Joined: Jul 2006
Posts: 150
Deutschland/Germany, nahe Hamb...
dennis Offline OP
Member
dennis  Offline OP
Member

Joined: Jul 2006
Posts: 150
Deutschland/Germany, nahe Hamb...
Hi!


I have got a problem with structs!

code:
"
#include <acknex.h>
#include <default.c>

typedef struct {
var Active;
var Active2;
var CountVis;
var CountInvis;
int Offset_1;
int Offset_2;
int Offset_3;
int Offset_4;
int Offset_5;
TEXT* Text_1;
TEXT* Text_2;
} LISTBOX;

function main()
{
LISTBOX* test;
test.Active = 1;
}
"

THIS IS WORKING....


code:
"
#include <acknex.h>
#include <default.c>

typedef struct {
var Active;
var Active2;
var CountVis;
var CountInvis;
int Offset_1;
int Offset_2;
int Offset_3;
int Offset_4;
int Offset_5;
TEXT* Text_1;
TEXT* Text_2;
} LISTBOX;

function main()
{
LISTBOX* arb;
arb.Active = 1;
}
"

THIS IS NOT WORKING....
(The application closes imediately after starting...the window hardly appears)

Seems to be a strange problem....

I hope somebody can help.

Re: Lite-C structs problem [Re: dennis] #137997
06/25/07 16:29
06/25/07 16:29
Joined: Jul 2000
Posts: 28,024
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 28,024
Frankfurt
This problem is not so strange - many beginners to C programming make the same mistake. Both codes obviously will just crash.

If you can't find the problem yourself, I'll give you a hint: You have defined a struct pointer, but where is its content?

Re: Lite-C structs problem [Re: dennis] #137998
06/25/07 16:36
06/25/07 16:36
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline
Expert
Joey  Offline
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
suppose the first one works because of the memory-alignment of structs. it's the first entry and thus starting at relative memory position 0x00... might work, is still wrong though.

Code:

function main()
{
LISTBOX* test = (LISTBOX*)malloc(sizeof(LISTBOX));
test.Active = 1;
}



joey.

Re: Lite-C structs problem [Re: Joey] #137999
06/25/07 17:20
06/25/07 17:20
Joined: Jan 2006
Posts: 1,829
Neustadt, Germany
T
TWO Offline

Serious User
TWO  Offline

Serious User
T

Joined: Jan 2006
Posts: 1,829
Neustadt, Germany
Or just

Code:

LISTBOX test;
test.Active = 1;



But who cares...

Re: Lite-C structs problem [Re: TWO] #138000
06/25/07 19:47
06/25/07 19:47
Joined: Jul 2006
Posts: 150
Deutschland/Germany, nahe Hamb...
dennis Offline OP
Member
dennis  Offline OP
Member

Joined: Jul 2006
Posts: 150
Deutschland/Germany, nahe Hamb...
Thanks, it worked!

However, i have a new problem now ....

code:
"
///////////////////////////////////////////////////////////////
#include <acknex.h>
#include <default.c>

typedef struct {

var Active;

} LISTBOX;

LISTBOX* ListBoxArray[100];

var Debug_1 = 0;

function extra()
{
while(1)
{
Debug_1 = ListBoxArray[0].Active;
wait(1);
}
}


function main()
{
level_load("");
wait(3);

LISTBOX Test;
ListBoxArray[0] = &Test;
ListBoxArray[0].Active = 1;
extra();

}
PANEL* Debug_Pan =
{
flags = VISIBLE;
digits = 100,100,8.4,*,1,Debug_1;
}
"

The panel should display "1"...I think...but it shows "2040952.2500"...
I am sure I there's something wrong, but I do not find it!

Using this code (with the loop in the main function) the panel displayed "1":
"
///////////////////////////////////////////////////////////////
#include <acknex.h>
#include <default.c>

typedef struct {

var Active;

} LISTBOX;

LISTBOX* ListBoxArray[100];

var Debug_1 = 0;

function main()
{
level_load("");
wait(3);

LISTBOX Test;
ListBoxArray[0] = &Test;
ListBoxArray[0].Active = 1;

while(1)
{
Debug_1 = ListBoxArray[0].Active;
wait(1);
}

}

PANEL* Debug_Pan =
{
flags = VISIBLE;
digits = 100,100,8.4,*,1,Debug_1;
}
"

Re: Lite-C structs problem [Re: dennis] #138001
06/26/07 08:14
06/26/07 08:14
Joined: Jan 2004
Posts: 2,013
The Netherlands
E
Excessus Offline
Expert
Excessus  Offline
Expert
E

Joined: Jan 2004
Posts: 2,013
The Netherlands
Probably something with operator precedence. "." and "[]" have the same precedence if I remember correctly.

So the correct code for accessing the Active member of the first element of the array is:
(ListBoxArray[0]).Active

Re: Lite-C structs problem [Re: Excessus] #138002
06/26/07 12:11
06/26/07 12:11
Joined: Jul 2006
Posts: 150
Deutschland/Germany, nahe Hamb...
dennis Offline OP
Member
dennis  Offline OP
Member

Joined: Jul 2006
Posts: 150
Deutschland/Germany, nahe Hamb...

@Excessus:
Well....I tried this but it did not work, too.

@All
I have written a bit more code so that you can see what I want to do exactly
and I have added comments for most of the functions.

I think I am doing the same fault as above but I could not think of something else.

If you know a solution you just correct the fault.

I can send you the .c-file, too.

Here's the code:
"
///////////////////////////////////////////////////////////////// Begin

// I want to write a script with functions that allow you to
// create ListBoxes (like in Microsoft VB) dynamically.


// I include the headers for Lite-C
#include <acknex.h>
#include <default.c>

// I define some variables
var Debug_1 = 0; // Debugging
var ListBoxID = 0; // Storing the ID of the created ListBox
var ListBoxCount = 0; // The amount of ListBoxes

// I define some function prototypes
function TestLoop();


// That's the struct "LISTBOX" with all important properties
// (I defined only the var "Active" for testing)
typedef struct {

var Active;

} LISTBOX;

// That's the pointer array for storing the pointers of
// created ListBoxes
LISTBOX* ListBoxArray[100];

// This is the most important function, I think.....
function NewListBox()
{

// Increase the amount of ListBoxes
ListBoxCount += 1;

// I create a new object with the type "ListBox"
// I do this just as in the manual (-> structs)
// < SPOT myspot; // creates an uninitalized SPOT struct named "myspot" >
LISTBOX Test; // (the same as TWO did)

// I suppose I could define the struct "Test" global...but there would be other problemes then


// This ListBox is not filled with any content, so I fill it
Test.Active = 50;

// ---> The ListBoxArray[100] is an array of 100 "ListBox"-pointers.
// ---> I convert the "ListBox"-struct "Test" into a "ListBox"-pointer
// ---> and let an element of the array point to it.
// This way I will (hopefully) be able to access this "ListBox"-struct later throught the array!
ListBoxArray[ListBoxCount] = &Test;


// Return the ID of the new ListBox
return(ListBoxCount);

}

// The main function in which I load the level and call the "NewListBox" function
function main()
{

// Load level
level_load("");
wait(3);

// Create new ListBox
ListBoxID = NewListBox();

// Change properties
(ListBoxArray[ListBoxID]).Active = 100;

// Call "TestLoop()"
TestLoop(ListBoxID);

}

// Is the ListBox created and do I have access to it?
// -> Then Debug_1 should be "100" (as set in main)
function TestLoop(var TestListBoxID)
{
while(1)
{

Debug_1 = (ListBoxArray[TestListBoxID]).Active;

wait(1);

}
}

// Debugging Panel
PANEL* Debug_Pan =
{
flags = VISIBLE;
digits = 100,100,8.4,*,1,Debug_1;
}

///////////////////////////////////////////////////////////////// End
"

Re: Lite-C structs problem [Re: dennis] #138003
06/26/07 12:22
06/26/07 12:22
Joined: Jul 2000
Posts: 28,024
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 28,024
Frankfurt
The problem is here:

function NewListBox()
{
...
LISTBOX Test; // (the same as TWO did)
...

Variables defined in a function are local. They only exist within that function, so your global struct pointer still has no content.

You can either define a global LISTBOX resp. an array of global listboxes, or use mem_alloc for locally allocating global memory as suggested above. Mem_alloc is something for advanced programmers; the easiest way for beginners is a global array. You don't need to care about any pointers and memory allocation then.

LISTBOX listbox[100];

Re: Lite-C structs problem [Re: jcl] #138004
06/26/07 14:19
06/26/07 14:19
Joined: Jul 2006
Posts: 150
Deutschland/Germany, nahe Hamb...
dennis Offline OP
Member
dennis  Offline OP
Member

Joined: Jul 2006
Posts: 150
Deutschland/Germany, nahe Hamb...
Yes! That's it!

Thanks a lot!

Okay...I have corrected the code using a global array.

The code has even become a bit shorter...

The application is running now and the displayed values are right.

I post the correction here.....(perhaps somebody has a similar problem...)

code:
"

(I have found another fault..I had left the brackets empty)
...
// I define some function prototypes
function TestLoop(var NewListBoxID);
...


(My main fault..the array is global now)
...
// That's the pointer array for storing the pointers of
// created ListBoxes
LISTBOX ListBoxArray[100];
...

...
// This is the most important function, I think.....
function NewListBox()
{

// Increase the amount of ListBoxes
ListBoxCount += 1;

// ---> The ListBoxArray[100] is a global array of 100 "ListBox"-pointers.
// ---> I set the properties of the global array to the default values...
// This way I will be able to access this new "ListBox" later through the array!
ListBoxArray[ListBoxCount].Active = 100;

// Return the ID of the new ListBox
return(ListBoxCount);

}
...


"

Re: Lite-C structs problem [Re: dennis] #138005
06/26/07 18:55
06/26/07 18:55
Joined: Jul 2000
Posts: 8,973
Bay Area
Doug Offline
Senior Expert
Doug  Offline
Senior Expert

Joined: Jul 2000
Posts: 8,973
Bay Area
I've seen professionals make the same mistakes as you are (heck, I slip now and then ). I think it is because they never sat down and learned what exactly is going on "under the code".

I would recommend going online and reading some guides on how C allocates memory (since Lite-C follows most of the same rules) and learn this now. You'll save a ton of time debugging later on (and you'll impress people at job interviews ).


Conitec's Free Resources:
User Magazine || Docs and Tutorials || WIKI
Page 1 of 2 1 2

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

Gamestudio download | 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