Nested TYPE arrays

New to FreeBASIC? Post your questions here.
Post Reply
gddeluca
Posts: 3
Joined: Jan 16, 2022 20:44

Nested TYPE arrays

Post by gddeluca »

I'm considering porting a large PowerBasic program to FB. But I need to handle nested arrays of UDT structures. I've been playing with a trial structure to get the syntax correct, but I can't get it to work.

Here's where I sit, and I've tried all kinds of variations of pointer coding, but none work. The example just loads an array within a single UDT structure and then prints it out. Can anyone give me a hint. Is it even possible?

Code: Select all

type LModel
   LTxt     as string
end type
   
type TabModel
   L(1 to 10) as LModel
end type   

dim TabTable(1 to 5) as TabModel 
Dim TP as TabModel ptr
dim as long i, j
j = 1
TP = @TabTable(j)

for i = 1 to 10
   *TP->L(i).LTxt = str$(i)
next i   

for i = 1 to 10
   print *TP->L(i).LTxt
next i   

Sleep
paul doe
Moderator
Posts: 1730
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Nested TYPE arrays

Post by paul doe »

Of course it is:

Code: Select all

type LModel
   LTxt     as string
end type
   
type TabModel
   L(1 to 10) as LModel
end type   

dim TabTable(1 to 5) as TabModel
Dim TP as TabModel ptr
dim as long i, j
j = 1
TP = @TabTable(j)

for i = 1 to 10
  TP->L(i).LTxt = str(i)
next

for i = 1 to 10
  print TP->L(i).LTxt
next

Sleep
You do understand how pointers work, yes? If not, have a look at the FreeBasic Manual for a primer on their usage:

https://www.freebasic.net/wiki/ProPgPointers
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Nested TYPE arrays

Post by badidea »

Note that the use of pointers can be avoided many cases. E.g.:

Code: Select all

type LModel
	LTxt as string
end type
   
type TabModel
	L(1 to 10) as LModel
end type   

dim TabTable(1 to 5) as TabModel
dim as long i, j = 1

for i = 1 to 10
	TabTable(j).L(i).LTxt = str(i)
next

for i = 1 to 10
	with TabTable(j)
		print .L(i).LTxt
	end with
next

Sleep
Mostly personal preference, but I think that it is easier to make errors with pointers.
gddeluca
Posts: 3
Joined: Jan 16, 2022 20:44

Re: Nested TYPE arrays

Post by gddeluca »

Paul: Yes, I understand pointers, it's syntax differences between PB and FB that are catching me. But why doesn't TP need the * in front, or is that implied by the -> syntax?

George
SARG
Posts: 1756
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Nested TYPE arrays

Post by SARG »

gddeluca wrote:But why doesn't TP need the * in front, or is that implied by the -> syntax?
p->member is equivalent to (*p).member so in your case * is useless.
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Nested TYPE arrays

Post by badidea »

See also section 'Pointers to user-defined types' on https://www.freebasic.net/wiki/ProPgPointers
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Nested TYPE arrays

Post by Munair »

As badidea said, you do not need pointers here. Instead of:

Code: Select all

Dim TP as TabModel ptr
TP = @TabTable
TP->L.LTxt = str(i)
this is easier and better (let the compiler handle the pointers):

Code: Select all

dim TP as TabModel
TP.L.LTxt = str(i)
gddeluca
Posts: 3
Joined: Jan 16, 2022 20:44

Re: Nested TYPE arrays

Post by gddeluca »

Thanks guys, got it now.
Post Reply