BBC BASIC for Windows
Programming >> BBC BASIC language >> Passing substructure arrays to procedures
http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1373610295

Passing substructure arrays to procedures
Post by Matt on Jul 12th, 2013, 06:24am

Hi,

In Wiki there is a section on passing substructures to procedures. In my program I want to copy an array from a structure rather than a substructure.

I've tried the following:
Code:
      DIM a{b(3)}, c(3)
      FOR I = 0 TO 3
        a.b(I) = I * 10
      NEXT
      PROC_TEST(a{}, a.b(), c())
      END
      
      DEF PROC_TEST(x{}, y(), RETURN z())
      !(^y()+4) += !(^x{}+4)
      FOR I = 0 TO 3
        z(I) = y(I)
      NEXT
      ENDPROC
 

However, I get a bad subscript error before going to the PROC (line 4).

Is there a way of doing this?

Matt
Re: Passing substructure arrays to procedures
Post by admin on Jul 12th, 2013, 07:51am

on Jul 12th, 2013, 06:24am, Matt wrote:
In my program I want to copy an array from a structure rather than a substructure.

Since substructures aren't involved at all, there's no complication and you don't need to take any special measures:

Code:
      DIM a{b(3)}, c(3)
      FOR I = 0 TO 3
        a.b(I) = I * 10
      NEXT
      PROC_TEST(a{}, c())
      END

      DEF PROC_TEST(x{}, z())
      LOCAL i
      FOR i = 0 TO 3
        z(i) = x.b(i)
      NEXT
      ENDPROC 


Richard.
Re: Passing substructure arrays to procedures
Post by Matt on Jul 13th, 2013, 05:48am

OK. That's useful to know. However, maybe I wasn't quite clear on what I want. Let me adjust the code slightly to elucidate.
Code:
      DIM a{b(3), d(3)}, c(3)
      FOR I = 0 TO 3
        a.b(I) = I * 10
      NEXT
      PROC_TEST(a.b(), c())
      ...
      REM do something with c()
      ...
      PROC_TEST(a.d(), c())
      ...
      REM do something with c()
      ...
      END

      DEF PROC_TEST(x(), z())
      LOCAL i
      FOR i = 0 TO 3
        z(i) = x(i)
      NEXT
      ENDPROC 


This won't work (obviously, otherwise I wouldn't be asking the question), but I hope you get the idea.

Matt
Re: Passing substructure arrays to procedures
Post by admin on Jul 13th, 2013, 09:13am

on Jul 13th, 2013, 05:48am, Matt wrote:
This won't work

The syntax a.b() is not valid in BBC BASIC; there is no way of representing a 'whole array' when it is a structure member. This is what it says in the BB4W Help: "Normally a structure member can be treated in exactly the same way as a simple variable of the same type. However this is not so in the case of operations on entire arrays or entire structures which cannot be used with structure members":

http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin2.html#members

It is, I would suggest, quite unusual to want to be able to perform the same operation on two different structure members (such as your a.b array and your a.d array). If the two arrays are sufficiently 'compatible' in their format and their usage that it is meaningful to perform the same operation, could you not combine them into a single two-dimensional array:

Code:
      DIM a{b(3,1)}, c(3)
      FOR I = 0 TO 3
        a.b(I,0) = I * 10
        a.b(I,1) = I * 11
      NEXT
      PROC_TEST(a{}, 0, c())
      
      REM do something with c()
      
      PROC_TEST(a{}, 1, c())
      
      REM do something with c()
      
      END
      
      DEF PROC_TEST(x{}, j, z())
      LOCAL i
      FOR i = 0 TO 3
        z(i) = x.b(i,j)
      NEXT
      ENDPROC 

Richard.
Re: Passing substructure arrays to procedures
Post by Matt on Jul 13th, 2013, 1:21pm

on Jul 13th, 2013, 09:13am, Richard Russell wrote:
It is, I would suggest, quite unusual to want to be able to perform the same operation on two different structure members (such as your a.b array and your a.d array).

I would normally agrre with you.

Quote:
If the two arrays are sufficiently 'compatible' in their format and their usage that it is meaningful to perform the same operation, could you not combine them into a single two-dimensional array.


I could do that, but I used seperate arrays due to naming. I'll have to use the code that I have been using, which is seperating them using CASE OF.

Thanks.

Matt