Wishlist for Structure like in C?

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
threedslider
Enthusiast
Enthusiast
Posts: 396
Joined: Sat Feb 12, 2022 7:15 pm

Wishlist for Structure like in C?

Post by threedslider »

Can we have Structures in PB like C programming for example :

Code: Select all

Structure Type
  x.f
  y.f
  z.f
EndStructure

Procedure.Type init(_x.f, _y.f, _z.f)
  var.Type
  
  var\x = _x
  var\y = _y
  var\z = _z
  
  ProcedureReturn var
EndProcedure

other_var.Type

other_var = init(0.0, 0.0, 0.0)
it is a wishlist for everyone and can make more easier in programming !

Please adding this feature ! :shock: :mrgreen:
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Wishlist for Structure like in C?

Post by DarkDragon »

There are structures like in C already?! Or do you mean structured return values? You can return a pointer.
bye,
Daniel
threedslider
Enthusiast
Enthusiast
Posts: 396
Joined: Sat Feb 12, 2022 7:15 pm

Re: Wishlist for Structure like in C?

Post by threedslider »

@DarkDragon : I need the function stuff for structure and return type as complex expression like to C but in PB :!:
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Wishlist for Structure like in C?

Post by DarkDragon »

threedslider wrote: Fri Jun 14, 2024 9:46 pm @DarkDragon : I need the function stuff for structure and return type as complex expression like to C but in PB :!:

Code: Select all

Structure Type
  x.f
  y.f
  z.f
EndStructure

Procedure.i init(_x.f, _y.f, _z.f)
  Protected *var.Type
  
  *var = AllocateStructure(Type)
  
  *var\x = _x
  *var\y = _y
  *var\z = _z
  
  ProcedureReturn *var
EndProcedure

Define *other_var.Type

*other_var = init(0.0, 1.0, 2.0)
If *other_var <> #Null
  Debug *other_var\x
  Debug *other_var\y
  Debug *other_var\z
  FreeStructure(*other_var)
EndIf
bye,
Daniel
threedslider
Enthusiast
Enthusiast
Posts: 396
Joined: Sat Feb 12, 2022 7:15 pm

Re: Wishlist for Structure like in C?

Post by threedslider »

Wow ! It is so flexible this PB !! :shock:

Thanks for your tips @DarkDragon !
User avatar
mk-soft
Always Here
Always Here
Posts: 6244
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Wishlist for Structure like in C?

Post by mk-soft »

Fast mode ...

Code: Select all


Structure sData
  x.f
  y.f
  z.f
EndStructure

DataSection
  mydefaultdata:
  Data.f 0.0, 1.0, 2.0
EndDataSection

Global myData.sData

CopyMemory(?mydefaultdata, @myData, SizeOf(myData))
Debug myData\x
Debug myData\y
Debug myData\z
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
threedslider
Enthusiast
Enthusiast
Posts: 396
Joined: Sat Feb 12, 2022 7:15 pm

Re: Wishlist for Structure like in C?

Post by threedslider »

Nice @mk-soft ! :shock:

Any is there to use a type of structure only without using the cost of memory ? For example returning only structure type without pointer ?

Thanks.
User avatar
mk-soft
Always Here
Always Here
Posts: 6244
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Wishlist for Structure like in C?

Post by mk-soft »

The struture type is only available at compile time.
There is no structure information at runtime. This means that no memory is used for it.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
sst
New User
New User
Posts: 5
Joined: Fri Jun 21, 2024 9:56 am

Re: Wishlist for Structure like in C?

Post by sst »

Another solution:

Code: Select all


EnableExplicit

Structure Type
  x.f
  y.f
  z.f
EndStructure


Procedure init(_x.f,_y.f,_z.f, *a.Type)
  *a\x=_x.f
  *a\y=_y.f
  *a\z=_z.f
 EndProcedure
  
  
 Define other_var.Type
 
 init(1,2,3,@other_var)
 
 
 Debug other_var\x                             ;1.0
 Debug other_var\y                             ;2.0
 Debug other_var\z                             ;3.0


threedslider
Enthusiast
Enthusiast
Posts: 396
Joined: Sat Feb 12, 2022 7:15 pm

Re: Wishlist for Structure like in C?

Post by threedslider »

@sst : Wow ! You are new and given the solution is great :shock:

Thank you a lot :mrgreen:
Post Reply