BBC BASIC for Windows
« strctures and arrays - a query »

Welcome Guest. Please Login or Register.
Apr 5th, 2018, 10:59pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

If you require a dump of the post on your message board, please come to the support board and request it.


Thank you Conforums members.

BBC BASIC for Windows Resources
Online BBC BASIC for Windows documentation
BBC BASIC for Windows Beginners' Tutorial
BBC BASIC Home Page
BBC BASIC on Rosetta Code
BBC BASIC discussion group
BBC BASIC for Windows Programmers' Reference

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: strctures and arrays - a query  (Read 510 times)
softweir
New Member
Image


member is offline

Avatar




PM


Posts: 12
xx strctures and arrays - a query
« Thread started on: Dec 7th, 2009, 3:46pm »

I am writing a graphics app based on an Iterative Function System. It would, for this, be convenient to be able to use arrays of arrays, and indeed BB4W does seem to offer the possibility, in the form of an array of structures, each of which has just one component - an array.

SO:

Code:
dim id(1,1)
id()=1.0,0.0,0.0,1.0

dim chromosome{(10) codon(1,1)}

for codon%=0 to 10
  chromosome{(codon%)}.codon()=id()
next
 


However, while
Code:
chromosome{(0)}.codon(0,0) = 0.0 
(for instance) works, the attempt to assign ...codon() as an entity fails with a Bad Subscript error.

I can see a number of ways around this including passing ^chromosome{(index)}.codon(0,0) to a procedure or function and accessing the memory directly, and using memory access (including ASM calls) to manipulate or use the sub-arrays, but is there a way to avoid this? Some minor trick of syntax which would enable me to, for instance, use the sub-arrays in dot-product calculations without bothering to write a dot-product routine?
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: strctures and arrays - a query
« Reply #1 on: Dec 7th, 2009, 4:30pm »

Quote:
but is there a way to avoid this? Some minor trick of syntax which would enable me to, for instance, use the sub-arrays in dot-product calculations

I'm afraid there isn't an elegant way; it's a side-effect of having to shoehorn structures into a language that was never designed to have them. Whereas in most respects structure members can be treated in the same way as ordinary objects of the same kind - and this is true of scalar variables - it's not the case for array or sub-structure members.

The technical explanation is that, with ordinary arrays, the array data immediately follows (in memory) the array descriptor, and operations such as the dot-product you need very much rely on this relationship. However in the case of an array within a structure the array data (which is part of the structure's data) is quite separate from the array descriptor (which is part of the structure's format).

Realistically, if you're to avoid assembler code, you will have to use an array of array pointers rather than an 'array of arrays':

Code:
      DIM id(1,1)
      id() = 1.0,0.0,0.0,1.0
      
      DIM chromosome(10)
      
      FOR codon% = 0 TO 10
        PROCdeclarearray(chromosome(codon%))
      NEXT
      
      FOR codon% = 0 TO 10
        PROCassignarray(chromosome(codon%), id())
      NEXT
      
      FOR codon% = 0 TO 10
        PROCprintarray(chromosome(codon%))
      NEXT
      
      END
      
      DEF PROCdeclarearray(RETURN codon())
      DIM codon(1,1)
      ENDPROC
      
      DEF PROCassignarray(RETURN codon(), id())
      codon() = id()
      ENDPROC
      
      DEF PROCprintarray(RETURN codon())
      PRINT codon(0,0) codon(0,1) codon(1,0) codon(1,1)
      ENDPROC 

I'm sorry that this is the best I can offer.

Richard.
« Last Edit: Dec 7th, 2009, 4:45pm by admin » User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: strctures and arrays - a query
« Reply #2 on: Dec 7th, 2009, 5:31pm »

I've written a quick Wiki article on the subject:

http://bb4w.wikispaces.com/Using+array+pointers

Richard.
User IP Logged

softweir
New Member
Image


member is offline

Avatar




PM


Posts: 12
xx Re: strctures and arrays - a query
« Reply #3 on: Dec 7th, 2009, 6:24pm »

Excellent! Thank-you for that Richard, that's just what I need. I was working thinking of trying to work towards something like that after reading the wiki on passing structure members to procedures, but this has saved me a considerable amount of time and experimentation.

Rick W
« Last Edit: Dec 7th, 2009, 6:49pm by softweir » User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls