Menu

Arrays Revisited

kent_twt4
2007-08-16
2013-05-30
  • kent_twt4

    kent_twt4 - 2007-08-16

    In a bit of a quandry on solving a coding issue.  First off, have identified a work around to make this work, but not what I was hoping for.  The offending code may well violate Basic programming rules, and or the GCBasic compiler?  The workaround is using Dim RxBuffer(2) and keeping BufferLen = 1.  Have tried using labels to get around this, but that doesn't seem to work.  Since this will be in a header file, was hoping to make the RxBuffer adjustable from 1 to max 80. 

    Dim RxBuffer(1)
    #define BufferLen 1

    For index = 1 to BufferLen
    RxBuffer(index) = SSPBUF
    Next

    ---------- Capture Output ----------
    > "C:\Program Files\GCBasic\Compile.bat" C:\PROGRA~1\CRIMSO~1\MarkIII\MSSP_I~1
    Great Cow BASIC (0.9 4/8/2007)

    Compiling C:\PROGRA~1\CRIMSO~1\MarkIII\MSSP_I~1 ...

    Errors have been found:

    MSSP_I~1 (28): Error: Array/Function RXBUFFER has not been declared
    MSSP_I~1 (28): Error: Invalid variable name: RXBUFFER(1)
    MSSP_I~1 (54): Error: Array/Function RXBUFFER has not been declared
    MSSP_I~1 (54): Error: Invalid variable name: RXBUFFER(1)
    MSSP_I~1 (58): Error: Array/Function RXBUFFER has not been declared
    MSSP_I~1 (68): Error: Array/Function RXBUFFER has not been declared
    MSSP_I~1 (68): Error: Invalid variable name: RXBUFFER(INDEX)

    The message has been logged to the file C:\PROGRA~1\GCBASIC\ERRORS.txt.

    Press any key to continue

     
    • Hugh Considine

      Hugh Considine - 2007-08-16

      This is due to the way GCBASIC handles variables and arrays internally. It does not distinguish between single element vars and arrays - all it stores is the number of elements. Thus, it thinks that a single element array is the same as a single element variable, which in a way it is. As you've discovered, the solution to this is to make the array 2 elements or replace it with a single element variable.

      If you're using a header file, you may consider passing the array to the various subroutines by reference. For example:

      #chip 16F88, 20

      'Create arrays, load in some values
      dim TestArray(10)
      dim OtherArray(20)
      For Element = 1 to 10
      TestArray(Element) = Element * Element
      Next
      For Element = 1 to 20
      OtherArray(Element) = Element
      Next

      'Write TestArray(5) to EEPROM location 0
      TestSub 0, TestArray(), 5
      'Write OtherArray(10) to EEPROM location 1
      TestSub 1, OtherArray(), 10

      sub TestSub (Location, SomeArray(), SomeElement)
      EPWrite Location, SomeArray(SomeElement)
      end sub

      While this increases the size of the sub call, it allows much greater flexibility. If you'd rather use a fixed array, then you can use a script like this:

      #define BufferLen 1
      Dim RxBuffer(RxBufferSize)
      #script
      RxBufferSize = BufferLen
      if BufferLen = 1 then RxBufferSize = 2
      'error Buffer Size BufferLen, Array Size RxBufferSize 'uncomment to see input and output of script in error listing
      #endscript

      Compiler scripts are run after the #defines have been read, but before any #defines have been applied to the program. While they are very limited, they're useful for cases like this where some calculation needs to be performed using constants.

       
    • kent_twt4

      kent_twt4 - 2007-08-16

      The #script does the job very well, Thanks!

      Have run into another problem and will post over on the developer forum, as its seems more appropriate?

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.