|
1 registered members (Grant),
999
guests, and 2
spiders. |
|
Key:
Admin,
Global Mod,
Mod
|
|
|
Pointer confusion?
#285790
08/20/09 07:51
08/20/09 07:51
|
Joined: Nov 2007
Posts: 1,143 United Kingdom
DJBMASTER
OP
Serious User
|
OP
Serious User
Joined: Nov 2007
Posts: 1,143
United Kingdom
|
Hi i'm a little confused about a pointer issue.
var sound_handle = 0 // variable sound handle
var* pnt_to_sound = &sound_handle; // pointer to handle
Ok, all well and good, the pointer is set to point to the variable. Now i'm trying to play a sound and return the handle into the var. Setting it directly...
sound_handle = snd_play("hi.wav",100,0);
no errors here. Setting it through the pointer...
*pnt_to_sound = snd_play("hi.wav",100,0);
Produces a crash. I thought it would work like this because dereferencing the pointer gets you the variable. I dont really work with pointers (i'm a C# guy), so i dont know what's wrong.
Last edited by DJBMASTER; 08/20/09 07:51.
|
|
|
Re: Pointer confusion?
[Re: DJBMASTER]
#299417
11/23/09 22:28
11/23/09 22:28
|
Joined: Nov 2007
Posts: 1,143 United Kingdom
DJBMASTER
OP
Serious User
|
OP
Serious User
Joined: Nov 2007
Posts: 1,143
United Kingdom
|
Didn't want to start a new post, so i'll resurrect my old one...
var sound_handle = 0; // variable sound handle
var* pnt_to_sound = &sound_handle; // pointer to handle
function TestPointer()
{
if(pnt_to_sound == NULL)
{
error("Null Pointer!");
}
else
{
*pnt_to_sound = snd_play("hi.wav",100,0);
}
}
void main()
{
on_space = TestPointer;
}
Pressing 'space' gives me the 'NUll Pointer' message, but i'm not sure why because it is clearly assigned an address.
|
|
|
Re: Pointer confusion?
[Re: pegamode]
#299421
11/23/09 22:54
11/23/09 22:54
|
Joined: Nov 2007
Posts: 1,143 United Kingdom
DJBMASTER
OP
Serious User
|
OP
Serious User
Joined: Nov 2007
Posts: 1,143
United Kingdom
|
In lite-c (as it is in c / c++) NULL is defined as 0.
So NULL == 0
Regards, Pegamode. Same thing happens if 'var sound_handle = 5;' Pointers are also only null when the pointer is set to 0, not the value it is referencing.
Last edited by DJBMASTER; 11/23/09 22:56.
|
|
|
Re: Pointer confusion?
[Re: Joozey]
#299427
11/23/09 23:10
11/23/09 23:10
|
Joined: Nov 2007
Posts: 1,143 United Kingdom
DJBMASTER
OP
Serious User
|
OP
Serious User
Joined: Nov 2007
Posts: 1,143
United Kingdom
|
It doesn't give me a NULL pointer meesage if the pointer is referenced inside a function...
var sound_handle = 0; // variable sound handle
var* pnt_to_sound; // pointer to handle
function TestPointer()
{
if(pnt_to_sound == NULL)
{
error("Null Pointer!");
}
}
void main()
{
pnt_to_sound = &sound_handle;
on_space = TestPointer;
}
Can't pointers be referenced in their definition in lite-c?
|
|
|
Re: Pointer confusion?
[Re: DJBMASTER]
#299429
11/23/09 23:38
11/23/09 23:38
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
having your pointer definition like so var* pnt_to_sound; // pointer to handle DESNT guarentee it is at zero if it hasn't been used yet.
Use this version to be sure. var* pnt_to_sound = NULL; // pointer to handle
AND remember to manually set the pointer back to NULL (or zero) when its been stopped.
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
|