Author |
Topic: strctures and arrays - a query (Read 510 times) |
|
softweir
New Member
member is offline


Posts: 12
|
 |
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?
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
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 » |
Logged
|
|
|
|
softweir
New Member
member is offline


Posts: 12
|
 |
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 » |
Logged
|
|
|
|
|