Pass structure to procedure to add, edit etc?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Pass structure to procedure to add, edit etc?

Post by Fangbeast »

How do you pass a structure via a pointer to a procedure?

I want to be able to add/edit/delete to/from a structured list without making everything global all the time.

I tried the below just as a start to access a structure (and I want to access a list later) but I get a "Pointer is Null" as I don't know what I am doing at all.

Code: Select all

Structure Testing
  Firstname.s
  Middlename.s
  Lastname.s
  Age.i
EndStructure

Procedure GetStuff(*Stuff.Testing)
  Debug *Stuff\Firstname  
EndProcedure

Define  *Foo.Testing\Firstname.s = "Gizmo Flarbit"  (Error occurs here)

GetStuff(@Foo)
Amateur Radio, D-STAR/VK3HAF
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Pass structure to procedure to add, edit etc?

Post by TI-994A »

Fangbeast wrote:...pass a structure via a pointer to a procedure?
Hi Fang. Try this:

Code: Select all

Structure Testing
  Firstname.s
  Middlename.s
  Lastname.s
  Age.i
EndStructure

Procedure GetStuff(*Stuff.Testing)
  Debug *Stuff\Firstname  
EndProcedure

Define Foo.Testing\Firstname.s = "Gizmo Flarbit"

GetStuff(@Foo)
To use pointers to structures, this should work:

Code: Select all

Structure Testing
  Firstname.s
  Middlename.s
  Lastname.s
  Age.i
EndStructure

Procedure GetStuff(*Stuff.Testing)
  Debug *Stuff\Firstname  
EndProcedure

*Foo.Testing = AllocateMemory(SizeOf(Testing))
*Foo.Testing\Firstname.s = "Gizmo Flarbit"

GetStuff(*Foo)
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Pass structure to procedure to add, edit etc?

Post by Demivec »

Here is a slightly easier way to use pointers to structures:

Code: Select all

Structure Testing
  Firstname.s
  Middlename.s
  Lastname.s
  Age.i
EndStructure

Procedure GetStuff(*Stuff.Testing)
  Debug *Stuff\Firstname  
EndProcedure

*Foo.Testing = AllocateStructure(Testing) ;this command also initializes the structure
*Foo.Testing\Firstname.s = "Gizmo Flarbit"

GetStuff(*Foo)

FreeStructure(*Foo) ;when done with the structure memory
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Pass structure to procedure to add, edit etc?

Post by wilbert »

Fangbeast wrote:I want to be able to add/edit/delete to/from a structured list without making everything global all the time.
I'm not sure if I understand your question correctly but to pass a list to a procedure, you don't need a pointer.

Code: Select all

Structure Animal
  Name.s
  Speed.l
EndStructure

Procedure SortAnimalsBySpeed(List Animals.Animal())
  SortStructuredList(Animals(), 0, OffsetOf(Animal\Speed), TypeOf(Animal\Speed))
EndProcedure

Procedure AddAnimal(List Animals.Animal(), Name.s, Speed.l)
  AddElement(Animals())
  Animals()\Name = Name
  Animals()\Speed = Speed
EndProcedure

Procedure Main()
  ; non global list named Animals
  Protected NewList Animals.Animal()
  
  AddAnimal(Animals(), "Tiger", 10)
  AddAnimal(Animals(), "Jaguar", 40)
  AddAnimal(Animals(), "Zebra", 30)
  SortAnimalsBySpeed(Animals())
  
  ForEach Animals()
    Debug Animals()\Name+" - Speed: "+Str(Animals()\Speed)
  Next
EndProcedure

Main()
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Pass structure to procedure to add, edit etc?

Post by Fangbeast »

wilbert wrote:
Fangbeast wrote:I want to be able to add/edit/delete to/from a structured list without making everything global all the time.
I'm not sure if I understand your question correctly but to pass a list to a procedure, you don't need a pointer.

Code: Select all

Structure Animal
  Name.s
  Speed.l
EndStructure

Procedure SortAnimalsBySpeed(List Animals.Animal())
  SortStructuredList(Animals(), 0, OffsetOf(Animal\Speed), TypeOf(Animal\Speed))
EndProcedure

Procedure AddAnimal(List Animals.Animal(), Name.s, Speed.l)
  AddElement(Animals())
  Animals()\Name = Name
  Animals()\Speed = Speed
EndProcedure

Procedure Main()
  ; non global list named Animals
  Protected NewList Animals.Animal()
  
  AddAnimal(Animals(), "Tiger", 10)
  AddAnimal(Animals(), "Jaguar", 40)
  AddAnimal(Animals(), "Zebra", 30)
  SortAnimalsBySpeed(Animals())
  
  ForEach Animals()
    Debug Animals()\Name+" - Speed: "+Str(Animals()\Speed)
  Next
EndProcedure

Main()
Holy cow, I did not know you could do that!! I looked through the manual but could not find this referenced anywhere (predictably as I didn't know what I was looking for as usual) but this will simplify things a lot.

Thank you.

And thanks to Demivec and TI-994A as well.

Now I suppose I have to actually start writing code instead of just designing forms (groaan).

P.S TI-994A, I know it's overkill but I am adding your banner that you made me to all of my programs as I like them and they are for my use anyway (heheheh).
Amateur Radio, D-STAR/VK3HAF
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Pass structure to procedure to add, edit etc?

Post by TI-994A »

Fangbeast wrote:...your banner that you made me...
So, it's your banner! :lol:

I'm really glad you like it.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Pass structure to procedure to add, edit etc?

Post by TI-994A »

Demivec wrote:

Code: Select all

*Foo.Testing = AllocateStructure(Testing) ;this command also initializes the structure
It should be noted that the AllocateStructure() function performs two processes; it allocates the memory required for the structure, and initializes arrays, lists, or maps in the structure, if any.

As such, for structures that don't utilise arrays, lists, or maps, the AllocateMemory() function would suffice.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Post Reply