Pointer Error

General FreeBASIC programming questions.
Dinosaur
Posts: 1358
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

Pointer Error

Postby Dinosaur » Nov 16, 2008 23:42

Hi all

I have tried for two days now to find the reason for this error, but my lack of experience with pointers is letting me down.

A slider routine is declared by Cgui as

Code: Select all

int AddSlider(int x,int y,int length,int *ctrl,int start,int end,int option,int id)
It is declared by FB as

Code: Select all

declare function AddSlider cdecl alias "AddSlider" (byval x as integer, byval y as integer, byval length as integer, byval ctrl as Integer ptr, byval start as integer, byval end as integer, byval option as integer, byval id as integer) as integer


I call it and place a slider on the screen, whick works, and allows me to change the slider. However the slider is suppose to update the variable pointed to.In this case Barpercent.

Code: Select all

Dim As Any Ptr BarPercent
Dim BarLoc As Integer Ptr
Dim BarPos As Integer

id = AddSlider (X,Y,Z,@BarPercent,1,2000,Option,0)

BarLoc = Barpercent
BarPos = *BarLoc
It is the last stement where I get an exception. I have tried a number of ways to overcome this, but to no avail.

Can sks steer me right here please.

Regards
McLovin
Posts: 82
Joined: Oct 21, 2008 1:15
Contact:

Postby McLovin » Nov 17, 2008 0:06

You seem to be passing an uninitialized pointer to the function. Try sending the variable address of a regualr integer variable to the function.

Instead of:

Dim As Any Ptr BarPercent

Try:

Dim As Integer BarPercent


Hope this works for you.

----
Fogell
Dinosaur
Posts: 1358
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

Postby Dinosaur » Nov 17, 2008 0:41

Hi all

Well , yes that worked.
But I dont understand why.
You seem to be passing an uninitialized pointer to the function.
Please tell me how to initialize the pointer.

I have done the same thing in other parts of my code, and they work, yet this one doesnt.??

REgards
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Postby MichaelW » Nov 17, 2008 4:10

According to the documentation for AddSlider:

ctrl: a pointer to some variable of your program which value controls the position of the slider handle, and that is affected by the user actions.

So BarPercent should be an integer, and you should pass the address of it to the function. The *BarLoc statement is trying to use the value of BarPercent as an address.
Dinosaur
Posts: 1358
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

Postby Dinosaur » Nov 17, 2008 4:20

Hi all

ctrl: a pointer to some variable of your program which value controls the position of the slider handle, and that is affected by the user actions.
I discussed the wording of this with the author, as it sounds like the value in ctrl decides the position of the slider.

Michael, I did work it out in the end, as most of the Cgui routines use pointers to other routines, this AddSlider actually updates my Variable, and that's why (as you pointed out) I can use the value in BarPercent directly.

Many thanks to all.

Regards

Return to “General”

Who is online

Users browsing this forum: No registered users and 13 guests