Gamestudio Links
Zorro Links
Newest Posts
Executing Trades on Next Bar Open
by Zheka. 06/20/24 14:26
Lapsa's very own thread
by rki. 06/19/24 11:27
A simple game ...
by VoroneTZ. 06/18/24 10:50
Face player all the time ...
by bbn1982. 06/18/24 10:25
Zorro Beta 2.61: PyTorch
by jcl. 06/10/24 14:42
New FXCM FIX Plugin
by flink. 06/04/24 07:30
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 678 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
squik, AemStones, LucasJoshua, Baklazhan, Hanky27
19060 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Handling of Pointers in Lite-C #452705
06/22/15 15:32
06/22/15 15:32
Joined: Mar 2014
Posts: 33
Germany: Sachsen
N
Nicros Offline OP
Newbie
Nicros  Offline OP
Newbie
N

Joined: Mar 2014
Posts: 33
Germany: Sachsen
Hi,
I am new to the lite-C pointer arithmetic.
I've heard that you don't have to dereference the pointers.
I get a error in the following source code. I already tried to fix it on my own but with no success. It's probably a simple mistake laugh
The code prints the error: Can't convert P_GT:Pointer:Long:Long
Code:
#include <acknex.h>
#include <default.c>

function main()
{
 // 'min' shall point on the value of the smallest variable
 var x = 5 ,y = 3;
 var* min;
 
 if(x < y)
    min = x;
 else
    min = y;
 
 // output 'min'
 printf(_chr(str_for_num(NULL,min)));
 
 // determine, whether 'min' is bigger than 4
 if (min > 4)
    printf(">4");
 else
    printf("<=4");
}


The output of 'min' is correct(3).
Where is the mistake ?

Regards Paul

Re: Handling of Pointers in Lite-C [Re: Nicros] #452706
06/22/15 15:53
06/22/15 15:53
Joined: Jul 2008
Posts: 2,107
Germany
rayp Offline

X
rayp  Offline

X

Joined: Jul 2008
Posts: 2,107
Germany
Here you go
Code:
void main(){
 // 'min' shall point on the value of the smallest variable
 var x   = 5;
 var y   = 3;
 var min = 0; 
 if(x < y) min = x;
 else      min = y;
 
 // output 'min'
 printf(_chr(str_for_num(NULL, min)));
 
 // determine, whether 'min' is bigger than 4
 if (min > 4) printf(">4");
 else         printf("<=4");
}


entity pointer example
Code:
void _bla(){
   ENTITY* entpointer;
   //or 4ex
   //ENTITY* entpointer = ent_create ( ... );
   if (entpointer) entpointer.skill1 = 1;
}



Greets


Acknex umgibt uns...zwischen Dir, mir, dem Stein dort...
"Hey Griswold ... where u gonna put a tree that big ?"
1998 i married my loved wife ... Sheeva from Mortal Kombat, not Evil-Lyn as might have been expected
rayp.flags |= UNTOUCHABLE;
Re: Handling of Pointers in Lite-C [Re: rayp] #452708
06/22/15 16:23
06/22/15 16:23
Joined: Mar 2014
Posts: 33
Germany: Sachsen
N
Nicros Offline OP
Newbie
Nicros  Offline OP
Newbie
N

Joined: Mar 2014
Posts: 33
Germany: Sachsen
But I want that 'min' is a pointer which points to the memory region of the smallest variable because I change the value of the smallest variable thousands oft times and I don't want to change it twice. Is this possible ?

Re: Handling of Pointers in Lite-C [Re: Nicros] #452709
06/22/15 17:21
06/22/15 17:21
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
Code:
#include <acknex.h>
#include <default.c>

function main()
{
 // 'min' shall point on the value of the smallest variable
 var x = 5 ,y = 3;
 var* min;
 
 if(x < y)
    min = &x;
 else
    min = &y;
 
 // output 'min'
 printf(_chr(str_for_num(NULL,*min)));
 
 // determine, whether 'min' is bigger than 4
 if (*min > 4)
    printf(">4");
 else
    printf("<=4");
}



Why on earth you would ever want to do anything like this, I have no idea, not even after your explanation. But pointer referencing and dereferencing works the same as in C.


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: Handling of Pointers in Lite-C [Re: Nicros] #452710
06/22/15 17:24
06/22/15 17:24
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Hi,
I sightly remember it working in A7 times confused I could be wrong.

Anyway, I feel a better option to write the full code instead of letting the compiler play in the dark.

Code:
#include <acknex.h>
#include <default.c>

function main()
{
 // 'min' shall point on the value of the smallest variable
 var x = 5 ,y = 3;
 var* min;
 
 if(x < y)
    min = &x;
 else
    min = &y;
 
 // output 'min'
 printf(_chr(str_for_num(NULL,*min)));
 
 // determine, whether 'min' is bigger than 4
 if (*min > 4)
    printf(">4");
 else
    printf("<=4");
}



edited: late xP

Last edited by txesmi; 06/22/15 17:27. Reason: late
Re: Handling of Pointers in Lite-C [Re: txesmi] #452711
06/22/15 17:36
06/22/15 17:36
Joined: Mar 2014
Posts: 33
Germany: Sachsen
N
Nicros Offline OP
Newbie
Nicros  Offline OP
Newbie
N

Joined: Mar 2014
Posts: 33
Germany: Sachsen
Thank you very much. That solved my problem. blush

Edit: So far so good. This example works fine, but it doesn't work in my main programm. Therefore I've searched the mistake in my programm and I came to the conclusion that the 'wait' command makes the pointer invalid.
What's the reason for this ?
New code:
Code:
#include <acknex.h>
#include <default.c>

function main()
{
 // 'min' shall point on the value of the smallest variable
 var x = 6,y = 5;
 var* min=NULL;
 
 if(x < y)
    min = &x;
 else
    min = &y;
 
 // output 'min'
printf(_chr( str_cat(str_create("Min: "), (str_for_num(NULL,*min) ))) );
 
 // determine, whether 'min' is bigger than 4
 if (*min > 4.0)
    printf(">4");
 else
    printf("<=4");
 printf(_chr( str_cat(str_create("Min: "), (str_for_num(NULL,*min) ))) ); // 5 -> ok  
 y+=5;
 printf(_chr( str_cat(str_create("Min: "), (str_for_num(NULL,*min) ))) ); // 10 -> ok
 wait(1);
 printf(_chr( str_cat(str_create("Min: "), (str_for_num(NULL,*min) ))) ); // rubish (often 0) -> wtf !?
}


Output: "Min: 5" , ">4" , "Min: 5" , "Min: 10" , "Min: 0"
Thanks for help.

Last edited by Nicros; 06/23/15 13:50. Reason: New problem :(
Re: Handling of Pointers in Lite-C [Re: Nicros] #452755
06/24/15 09:51
06/24/15 09:51
Joined: Sep 2003
Posts: 9,859
F
FBL Offline
Senior Expert
FBL  Offline
Senior Expert
F

Joined: Sep 2003
Posts: 9,859
To me this seems like a problem with pushing to and popping from stack during wait.
wait() actively halts the function in execution and any local variables are put to the stack, so their values are not lost.
This way other functions can run during the wait() command.
However when the execution of the function is continued afterwards, the local variables are popped from the stack and may now be at a different address, making the pointer invalid.

justsid possibly can explain in a more correct and more technical way, but this is what I assume is happening.

In short: Might be a bug with the compiler (or simply not supported, and I'm not even sure if it can be done so easily out of the box)...
This is also important to know when calling functions with local pointers as parameters. Once the called function uses wait() the pointers will screw up. I think this is even mentioned somewhere in the manual.

Lesson learned: don't write code like this. wink

Code:
#include <acknex.h>
#include <default.c>

function main()
{
 // 'min' shall point on the value of the smallest variable
 var x = 6,y = 5;
 var* min=NULL;
 
 if(x < y)
    min = &x;
 else
    min = &y;
 
 // output 'min'
printf(_chr( str_cat(str_create("Min: "), (str_for_num(NULL,*min) ))) );
 
 // determine, whether 'min' is bigger than 4
 if (*min > 4.0)
    printf(">4");
 else
    printf("<=4");
 printf(_chr( str_cat(str_create("Min: "), (str_for_num(NULL,*min) ))) ); // 5 -> ok  
 y+=5;
 printf(_chr( str_cat(str_create("Min: "), (str_for_num(NULL,*min) ))) ); // 10 -> ok
 printf("ptr min 0x%x\nadr y 0x%x", (int)min, (int)&y);
 wait(1);
 printf(_chr( str_cat(str_create("Min: "), (str_for_num(NULL,*min) ))) ); // rubish (often 0) -> wtf !?
 printf("ptr min 0x%x\nadr y 0x%x", (int)min, (int)&y);
 
}



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