News:

Let's find out together what makes a PIC Tick!

Main Menu

Alias for an array

Started by keytapper, Apr 14, 2024, 02:45 PM

Previous topic - Next topic

keytapper

Hi there,
I was expecting to make an alias from a bigger array.

Symbol ARRAYLEN 64
Symbol MIDARRAY ARRAYLEN / 2

Dim person[ARRAYLEN] As Byte
' just an example to look into the second half
Dim surname As array.MIDARRAY

Str person = "Billy",0
Str surname = "Unknown",0

I think is not there, but simple to implement. Accepting the fact that only constant are allowed to address the alias. In fact, in the assembler each byte is well defined.

The only advantage is to look into an allocated block of RAM to modify part of it. The idea is to avoid to make two separated arrays, because there's a difficulty to make it like a record.
But I doubt if it is useful or I'm looking for an useless complication  ;D
Ignorance comes with a cost

Stephen Moss

Not sure what you are trying to achieve here, but it sounds like you are trying to create a two dimantional array in a single array rather than using two differnt arrays, storing the Forename in one half of the array and Surname in the the other depending on which you are searching for would it not be something like...

Symbol ARRAYlen as Byte = 64
Symbol MIDARRAY as byte ARRAYlen / 2
Dim MyArray[ARRAYlen] as Byte  'Create a 64 byte array
Dim Person as byte

'Find/Search by Forname
For Person = 0 to MIDARRAY-1
  Person = MyArray[Person]              'Get/Set Forename location within the array
  Surname = MayArray[Person + MIDARRAY] 'Get/Set Surname location within the array

  'Do something with the array location data here
Next

'Find/Search by SurName
For SurName = MIDARRAY to ARRAYlen -1
  Surname = MayArray[SurName]           'Get/Set Surname location within the array
  Person = MyArray[Surname - MIDARRAY]  'Get/Set Forename location within the array
 

  'Do something with the array location data here
Next

If your are only Storing ForeNames and Surnames when why not keep them together, i.e. Forename 1 = Array,0 and Surname 1 = Array,1. To enter/retrieve data from the would be something like...
For X = 0 to ARRAYlen step 2
   Person = MyArray[X]      'Get/Set Forename location within the array
   Surname = MyArray[X + 1] 'Get/Set SurName location within the array

  'Do something with the array location data here
Next
 

keytapper

I also came across this fact, which might be one of the convenient option.
Well, it's not a good point to make a subarray by an alias.
Ignorance comes with a cost

top204

If it is String arrays you are wanting, I did create a simple, experimental, String array set of meta-macros a few years ago, that works well.

Because of the RAM mechanisms in PICmicro devices, the String array meta-macros only work with 18F devices.

Below is a demo code listing showing how to use the String Array routines, and it also allows for flash memory strings:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Demonstrate RAM and Flash memory string array handling using the "String Array.inc" meta-macros
' For 18F devices only
'
' Written by Les Johnson for the Positron8 BASIC compiler
'
    Device = 18F25K20                                               ' Tell the compiler what device to compile for
    Declare Xtal = 16                                               ' Tell the compiler what frequency the device is operating at (in MHz)
'
' Setup USART1
'
    Declare Hserial1_Baud = 9600
    Declare HRSOut1_Pin = PORTC.6

    Include "String Array.inc"                                      ' Load the String array macros into the program
'
' Create a 10 string flash memory array, with each String 24 characters in length (including the null)
' The flash memory string must be an even amount of bytes (including the null)
'
    Create_FlashStringArray(MyFlashString, 10, 24)= {"My Flash String Array 0", 0,
                                                     "My Flash String Array 1", 0,
                                                     "My Flash String Array 2", 0,
                                                     "My Flash String Array 3", 0,
                                                     "My Flash String Array 4", 0,
                                                     "My Flash String Array 5", 0,
                                                     "My Flash String Array 6", 0,
                                                     "My Flash String Array 7", 0,
                                                     "My Flash String Array 8", 0,
                                                     "My Flash String Array 9", 0}
'
' Create variables here
'
    Dim bIndex As Byte                                              ' Temporary byte used by the demo
    Dim TempString As String * 32 Heap                              ' Temporary string used by the demo
'
' Create a 10 string RAM array, with each string 20 characters in length (not including the null)
'
    Create_StringArray(MyString, 10, 20) = {"RAM String Array   0", 0,
                                            "RAM String Array   1", 0,
                                            "RAM String Array   2", 0,
                                            "RAM String Array   3", 0,
                                            "RAM String Array   4", 0,
                                            "RAM String Array   5", 0,
                                            "RAM String Array   6", 0,
                                            "RAM String Array   7", 0,
                                            "RAM String Array   8", 0,
                                            "RAM String Array   9", 0}

'--------------------------------------------------------------------------------------
' Test the string array meta-macros by loading then retrieving values
'
Main:
'
' Read the RAM String array's default content
'
    For bIndex = 0 To 9
        TempString = Read_StringArray MyString, [bIndex]            ' Retrieve the RAM string array
        HRSOutLn TempString                                         ' Transmit the string's text to a serial terminal
    Next
'
' Load and Read the RAM String array
'
    Clear MyString
    For bIndex = 0 To 9
        TempString = "MyString RAM Array " + Str$(Dec bIndex)       ' Fill the string to load and retrieve
        Write_StringArray MyString,[bIndex], TempString             ' Load the RAM string array
        TempString = Read_StringArray MyString, [bIndex]            ' Retrieve the RAM string array
        HRSOutLn TempString                                         ' Transmit the string's text to a serial terminal
    Next
'
' Read the Flash memory String array
'
    For bIndex = 0 To 9
        TempString = Read_FlashStringArray MyFlashString, [bIndex]  ' Retrieve a Flash memory string array
        HRSOutLn Dec bIndex, "...", TempString                      ' Transmit the string's text to a serial terminal
    Next

I have attached the String Array sources and the demo and a proteus project showing the above demo working in the simulator:

keytapper

Many thanks for your attention, Mr Les.
I admit that I should dig some more into the given includes.  :-[
Well, my attempt is to make a double buffering and trying to catch all income data. I started to think to use a swapping method so when one buffer still reading, the other will allow one more stream to come in.
As one condition will determine the end of message, then the swap will take place.

I think I looked for a complication.
Ignorance comes with a cost