1 registered members (AndrewAMD),
678
guests, and 6
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Handling of Pointers in Lite-C
#452705
06/22/15 15:32
06/22/15 15:32
|
Joined: Mar 2014
Posts: 33 Germany: Sachsen
Nicros
OP
Newbie
|
OP
Newbie
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 The code prints the error: Can't convert P_GT:Pointer:Long:Long
#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]
#452709
06/22/15 17:21
06/22/15 17:21
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
#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
Serious User
|
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
Hi, I sightly remember it working in A7 times ![confused confused](/ubb7/images/graemlins/default_dark/confused.gif) I could be wrong. Anyway, I feel a better option to write the full code instead of letting the compiler play in the dark.
#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
Nicros
OP
Newbie
|
OP
Newbie
Joined: Mar 2014
Posts: 33
Germany: Sachsen
|
Thank you very much. That solved my problem. ![blush blush](/ubb7/images/graemlins/default_dark/blush.gif) 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:
#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
FBL
Senior Expert
|
Senior Expert
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 wink](/ubb7/images/graemlins/default_dark/wink.gif)
#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);
}
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|