Static Array for Pointers

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Static Array for Pointers

Post by Josh »

If I have a memory area, either read in from a file or from the DataSection, that is constructed like an array, it would be fine to have an extension for pointers as exists within structures.

Code: Select all

*Ptr.MyStruc[0]
This would simplify the handling a lot and make the code more clear.
sorry for my bad english
User avatar
NicTheQuick
Addict
Addict
Posts: 1227
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Static Array for Pointers

Post by NicTheQuick »

Are you aware of the workaround?

Code: Select all

Structure IntegerList
	i.i[0]
EndStructure

Define *p.IntegerList

*p = ?mydata

Debug *p\i[0]
Debug *p\i[1]
Debug *p\i[2]
Debug *p\i[3]
Debug *p\i[4]
Debug *p\i[5]


DataSection
	mydata:
		Data.i 4, 6, 8, 2, 4, 12
EndDataSection
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Static Array for Pointers

Post by Josh »

Of course I'm aware of that, but introducing an additional structure doesn't necessarily make the code better:

Code: Select all

Structure MYSTRUC
  a.i
  b.i
EndStructure

Structure MYSTRUCARRAY
   MyStruc.MYSTRUC[0]
EndStructure

Define *p.MYSTRUCARRAY

*p = ?mydata

Debug *p\MyStruc[0]\a
Debug *p\MyStruc[0]\b
Debug *p\MyStruc[1]\a
Debug *p\MyStruc[1]\b
Debug *p\MyStruc[2]\a
Debug *p\MyStruc[2]\b


DataSection
   mydata:
      Data.i 4, 6, 8, 2, 4, 12
EndDataSection
Or simpler:

Code: Select all

Structure MYSTRUC
  a.i
  b.i
EndStructure

Define *p.MYSTRUC[0]

*p = ?mydata

Debug *p[0]\a
Debug *p[0]\b
Debug *p[1]\a
Debug *p[1]\b
Debug *p[2]\a
Debug *p[2]\b


DataSection
   mydata:
      Data.i 4, 6, 8, 2, 4, 12
EndDataSection
sorry for my bad english
User avatar
NicTheQuick
Addict
Addict
Posts: 1227
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Static Array for Pointers

Post by NicTheQuick »

It would be nice to have this functionality like C has it. But what should we do in this case? Double indexed arrays?

Code: Select all

Structure myData
	*list.i[2]
EndStructure

Define myData.myData

myData\list[0] = ?mydata0
myData\list[1] = ?mydata1

Debug myData\list[0][0] ;=4
Debug myData\list[0][1] ;=6
Debug myData\list[1][0] ;=5
;...


DataSection
	mydata0:
		Data.i 4, 6, 8, 2, 4, 12
	mydata1:
		Data.i 5, 7, 9, 3, 5, 13
EndDataSection
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Static Array for Pointers

Post by Mijikai »

NicTheQuick wrote:...Double indexed arrays?
...
Yeah, it really sucks to use matrices with PB...
User avatar
mk-soft
Always Here
Always Here
Posts: 5402
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Static Array for Pointers

Post by mk-soft »

Only this way ...

Code: Select all

Structure ArrayOfInteger
   iVal.i[0]
EndStructure

Structure myData
  *list.ArrayOfInteger[3]
EndStructure

Define myData.myData

myData\list[0] = ?mydata0
myData\list[1] = ?mydata1
myData\list[2] = ?mydata2


Debug myData\list[0]\iVal[0] ;=4
Debug myData\list[0]\iVal[1] ;=6
Debug myData\list[1]\iVal[0] ;=5
Debug myData\list[2]\iVal[8] ;=116
;...


DataSection
   mydata0:
      Data.i 4, 6, 8, 2, 4, 12
   mydata1:
      Data.i 5, 7, 9, 3, 5, 13
   mydata2:
      Data.i 15, 17, 19, 13, 15, 113, 114, 115, 116
   
EndDataSection
or static column size

Code: Select all

Structure ArrayOfInteger
   iVal.i[0]
EndStructure

Structure myDataColumn
   col.i[6]
EndStructure


Structure myData
  row.myDataColumn[0]
EndStructure

Define *myData.myData

*myData = ?mydata0


Debug *myData\row[0]\col[0] ;=4
Debug *myData\row[0]\col[1] ;=6
Debug *myData\row[1]\col[0] ;=5
Debug *myData\row[2]\col[5] ;=113
;...

DataSection
   mydata0:
      Data.i 4, 6, 8, 2, 4, 12
      Data.i 5, 7, 9, 3, 5, 13
      Data.i 15, 17, 19, 13, 15, 113
   
EndDataSection
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
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Static Array for Pointers

Post by Josh »

I wasn't thinking so much about arrays with this post, certainly not multidimensional arrays. I was mainly concerned with the function as it is possible within structures with a static array [0].

With this you can organize so many things easily and clearly. For pointers this could be used well without the need for additional structures.


Especially for a look forward or backward, it has a special charm:

Code: Select all

Define *x.Character

x$ = "sdfs iotue rgdg" + #CRLF$ + "sdfsdf"
*x = @x$


For i = 1 To Len (x$)
  If *x\c = #CR And *x[1]\c = #LF
    Break
  Wend
Next

P.S.: In my first post I didn't even think about the fact that the [0] in the 'Define' statement wouldn't be necessary at all.
sorry for my bad english
User avatar
mk-soft
Always Here
Always Here
Posts: 5402
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Static Array for Pointers

Post by mk-soft »

Code: Select all

Structure ArrayOfCharacater
  c.c[0]
EndStructure

Define *x.ArrayOfCharacater

x$ = "sdfs iotue rgdg" + #CRLF$ + "sdfsdf"
*x = @x$


For i = 0 To Len(x$) - 1
  If *x\c[i] = #CR And *x\c[i+1] = #LF
    Debug "Break"
    Break
  EndIf
Next
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
Post Reply