Filling an array


+ Reply to Thread
Results 1 to 16 of 16
  1. #1
    Join Date
    Sep 2007
    Location
    Waco, Texas
    Posts
    156

    Default Filling an array

    Happy New Year to All-
    I am trying to do a simple dumb thing and the complier barks at me. I am trying to fill an array with hex numbers.
    Example below:
    x var byte 'Loop counter var
    L0B0 var byte[16] ' Loop Zero, Board Zero port array
    For x=1 to 16
    Lookup x,[$80,$80,$80,$80,$81,$80,$80,$81,$80,$80,$80,$81,$8 0,$80,$80,$81],L0B0[x]
    next x

    Error I get is:
    Output parameter must be a variable

    I am certain I have stared at it so long that I don't see the obvious so I am reaching out.
    Regards to All!
    "If we knew what we were doing, it wouldn't be called research"
    - Albert Einstein

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,172

    Default Re: Filling an array

    Quote Originally Posted by ecoli-557 View Post
    Happy New Year to All-
    I am trying to do a simple dumb thing and the complier barks at me. I am trying to fill an array with hex numbers.
    Example below:
    x var byte 'Loop counter var
    L0B0 var byte[16] ' Loop Zero, Board Zero port array
    For x=1 to 16
    Lookup x,[$80,$80,$80,$80,$81,$80,$80,$81,$80,$80,$80,$81,$8 0,$80,$80,$81],L0B0[x]
    next x

    Error I get is:
    Output parameter must be a variable

    I am certain I have stared at it so long that I don't see the obvious so I am reaching out.
    Regards to All!
    1. You have a space "$8 0" in your lookup.
    This causes an error.

    2. Use a temp variable.


    Code:
    x var byte 'Loop counter var
    L0B0 var byte[16] ' Loop Zero, Board Zero port array
    Temp var byte
    
    For x=1 to 16
        Lookup x,[$80,$80,$80,$80,$81,$80,$80,$81,$80,$80,$80,$81,$80,$80,$80,$81],temp
        L0B0[x] = temp
    next x
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  3. #3
    Join Date
    Sep 2007
    Location
    Waco, Texas
    Posts
    156

    Default Re: Filling an array

    Thanks Sayzer- I had corrected the space issue. Was not aware of the finer points of having to use a var as an interim..... Nice catch and thankful
    "If we knew what we were doing, it wouldn't be called research"
    - Albert Einstein

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,212

    Default Re: Filling an array

    Also, arrays are indexed starting at 0.
    Writing to L0B0[16] will be writing outside of the array corrupting whatever might be there.

    /Henrik.

  5. #5
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,172

    Default Re: Filling an array

    Quote Originally Posted by HenrikOlsson View Post
    Also, arrays are indexed starting at 0.
    Writing to L0B0[16] will be writing outside of the array corrupting whatever might be there.

    /Henrik.
    It is sad that the compiler does not throw an error about this issue.
    I think some compilers warn about it.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,212

    Default Re: Filling an array

    In this basic example the compiler could/would know but in many other cases it simply can't know, before hand, what the index might be (as it can be a result of a calculation for example). So it's more of a run-time thing than a compile-time thing.

    /Henrik.

  7. #7
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,172

    Default Re: Filling an array

    Quote Originally Posted by HenrikOlsson View Post
    In this basic example the compiler could/would know but in many other cases it simply can't know, before hand, what the index might be (as it can be a result of a calculation for example). So it's more of a run-time thing than a compile-time thing.

    /Henrik.
    I would not blame it for something it did not know.

    But I think that Microchip XC8 and other XCs (Pro versions) are checking this type of issues.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  8. #8
    Join Date
    Aug 2003
    Location
    Australia
    Posts
    1,003

    Default Re: Filling an array

    C compilers only know the size of an array if it is explicitly declared (which it always is in BASIC),
    but XC16 certainly doesn’t give a warning or an error, no matter how the array is declared. It will just read or write the memory after the array.
    I use this “feature” to dump memory. You could probably do that in PBP too. You just won’t be able to declare your array before system variables.

    For other platforms that run an OS, it’s sometimes also a way to look at memory you aren’t supposed to see, or inject code where you aren’t supposed to.

  9. #9
    Join Date
    Aug 2003
    Location
    Australia
    Posts
    1,003

    Default Re: Filling an array

    Ps.. I’d expect a C compiler error for using a constant index like this (expressed in BASIC) :
    Code:
    var byte array [8]
    
    array[23] = 99

  10. #10
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    2,790

    Default Re: Filling an array

    But, does it?

    Ioannis

  11. #11
    Join Date
    May 2013
    Location
    australia
    Posts
    1,559

    Default Re: Filling an array

    But, does it?
    xc8 no
    mikroc no
    sourceboost c no
    arduino no

    i have not encountered an 8 bit compiler that cares at all
    python yes
    This is more entertaining than Free to Air TV

  12. #12
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    2,790

    Default Re: Filling an array

    Seems the most logic.

    Art wished for this as his example was based on constants, but if the compiler does not check for this in the first place, why care for constants?

    Ioannis

  13. #13
    Join Date
    May 2013
    Location
    australia
    Posts
    1,559

    Default Re: Filling an array

    actually the latest xc8 gives a warning for a const index ,
    main.c:24: warning: (360) array index out of bounds

    but compiles happily anyway
    This is more entertaining than Free to Air TV

  14. #14
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    2,790

    Default Re: Filling an array

    That is very helpful! Although I am not in for C...

    Ioannis

  15. #15
    Join Date
    Aug 2003
    Location
    Australia
    Posts
    1,003

    Default Re: Filling an array

    I’d have expected PBP to check it if the index was constant. It obviously has the information to do so.
    I’ve been caught out by mixing up dec & hex expression before, resulting in access out of bounds.

    XC16 does check if the index is a constant: main.c:1896:5: warning: array subscript is above array bounds

    It does compile too, so I expect it to simply be looking into some other array.

  16. #16
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    2,790

    Default Re: Filling an array

    Even a warning is a good thing to have.

    It saves many hours of debugging if there is an error like mixing hex/dec as you said.

    At least for constants it can be implemented.

    Ioannis

Similar Threads

  1. filling an array
    By Beginner in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 11th August 2010, 14:19
  2. Copying Array to Array
    By vamtbrider in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 24th April 2010, 01:12
  3. Filling pdu array with gps data!
    By camolas in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 4th October 2008, 00:54
  4. Custom array of ports using array?
    By blainecf in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 18th June 2006, 01:43
  5. Word array behaving like byte array.... wierd
    By forgie in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 2nd August 2005, 15:43

Members who have read this thread : 23

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts