|
1 registered members (AndrewAMD),
4,408
guests, and 0
spiders. |
|
Key:
Admin,
Global Mod,
Mod
|
|
|
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
Expert
|
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: TWO]
#138000
06/25/07 19:47
06/25/07 19:47
|
Joined: Jul 2006
Posts: 150 Deutschland/Germany, nahe Hamb...
dennis
OP
Member
|
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: Excessus]
#138002
06/26/07 12:11
06/26/07 12:11
|
Joined: Jul 2006
Posts: 150 Deutschland/Germany, nahe Hamb...
dennis
OP
Member
|
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: jcl]
#138004
06/26/07 14:19
06/26/07 14:19
|
Joined: Jul 2006
Posts: 150 Deutschland/Germany, nahe Hamb...
dennis
OP
Member
|
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
Senior Expert
|
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  ).
|
|
|
|