View previous topic :: View next topic |
Author |
Message |
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Mon Oct 06, 2014 2:03 am Post subject: Questions regardling Structures, overlays and arrays |
|
|
Hello all,
I have a couple of questions to which I am hoping someone might be able to offer a suggestion. They are:
1. Multi-Dimensional arrays
I am trying to create a multi-dimensional structure to work as a buffer in a form such as this VB6 Code:
Code: |
Type typeTx_Buff
Dim Tx_Cmd_Wireless_Addr As Word
Dim Tx_Cmd_Wireless_Zone As Byte
Dim Tx_Cmd_Wireless_bCount As Byte
Dim Tx_Cmd_Wireless_CmdAs Byte
Dim Tx_Cmd_Wireless_Data As Byte
Dim Tx_Cmd_Wireless_Data1 As Byte
End Type
Public TxBuffer(100) As typeTx_Buff
|
This is my current work around for the structure:
Code: |
Dim Tx_Cmd_Wireless_Addr(20) As Word
Dim Tx_Cmd_Wireless_Addr_lobyte As Byte At Tx_Cmd_Wireless_Addr Overlay
Dim Tx_Cmd_Wireless_Addr_hibyte As Byte At Tx_Cmd_Wireless_Addr + 1 Overlay
Dim Tx_Cmd_Wireless_Zone(20) As Byte
Dim Tx_Cmd_Wireless_bCount(20) As Byte
Dim Tx_Cmd_Wireless_Cmd(20) As Byte
Dim Tx_Cmd_Wireless_Data(20) As Byte
Dim Tx_Cmd_Wireless_Data1(20) As Byte
|
I am hoping that someone may have a better idea/way to create a structure like this.
2. How to overlay a String Var over a Byte array
OverLay:
Code: |
Dim Tx_Cmd_Wireless_Addr(20) As Word
Dim Tx_Cmd_Wireless_Addr_Str As String * 4 At Tx_Cmd_Wireless_Addr(1) Overlay
|
The question is, how to repeat the overlay but for a specific element of array "Tx_Cmd_Wireless_Addr(20) ". As an
alternate, would it be better to simply use another byte varable with a string overlay, then use MEMCOPY to copy the desired byte array element
to this other byte variable for which a String Overlay already exists? This would be slower and take more memory I believe but may be a
viable alternative.
3. Is it possible to redim an array on the fly (I think not but maybe there is a way I am not aware of).
Thanks in advance all!
Tim
(BASCOM-AVR version : 2.0.7.7 ) |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Mon Oct 06, 2014 11:46 pm Post subject: |
|
|
1. Bascom knows basically only one-dimensional arrays. More dimensions have to be set up and handled by user code. The only two-dimensional built-in array I know, would be an array of strings. There the index is the first dimension, while the addressed string itself is the second dimension. But one needs to use then string functions, while one wants to work maybe with a byte array. The terminator byte is also a waste then and I feel it too complicated. Before using such workarounds, I'd have already written code, to handle these multiple dimensions and project it onto one large byte array.
2. An overlay uses the same memory space of one variable, by laying over another variable on it. That's where the name comes from. The overlaying variable can be of a different type than the overlaid one.
The shown sample makes little sense, while it can be done. However, I miss the meaning of overlaying a single string with length of 5 chars (included terminator) onto a word array of 20 words.
Overlay does not create a structure, nor can it be repeated by any parameters.
3. No. |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Tue Oct 07, 2014 8:30 pm Post subject: |
|
|
just want to add that in the next release arrays can have multiple dimensions like a(1,2,3,4,5). _________________ Mark |
|
Back to top |
|
|
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Thu Oct 09, 2014 11:35 pm Post subject: |
|
|
Hi all,
@ Mark - that will be a really cool addition!
@MWS the overlay is for a single word length, not all of them. It is for an array holding 20 descrete addresses each of 2 bytes.
For example,
Tx_Cmd_Wireless_Addr(1) = &hCDEF
The string is to hold the 4 characters 'CDEF' in this case.
Tim |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Fri Oct 10, 2014 12:21 am Post subject: |
|
|
You have a serious misunderstanding if you think a 4 character string overlayed onto a word will hold the characters 'CDEF' when the value of the word is &HCDEF. _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Fri Oct 10, 2014 1:13 am Post subject: |
|
|
HI Adrian,
That was exactly why I posted the question! Normally the overlay would cover the entire 80 bytes (+1) for the string overlay. So how to do it using just 1 element of the word array??? The only other way I could think of was to have a temp word variable, and overlay that. But that also makes for a lot of manipulation. So was looking for the correct and better way.
Tim |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Fri Oct 10, 2014 6:45 am Post subject: |
|
|
No Tim,
You still have it wrong. If you have a word ( 2 bytes ) with value &hCDEF, then there is no direct way using an overlay that you will ever see the representation of its value as a (4 byte) string 'CDEF'. The only way to get that is to do
Dim MyString as string * 4
Dim myWord as word
MyString = hexval(myWord )
which invokes a routine to convert a word value into its (hex) string representation.
Think of the degenerate case where the word value is &h0000 ( ie binary zero ). Then any sort of overlay would put two nulls ( binary zero ) into the first two bytes of the string. But no string can ever contain a null, since that is the string ender character.
The string representation of a binary zero word would be '0000', which is 4 ASCII characters of value 48 decimal ( &h30 ). But Bascom would suppress the leading zeroes of this, and only show the string as '0', ie one character of decimal value 48.
Get it ? _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Fri Oct 10, 2014 6:55 am Post subject: |
|
|
Hi Adrian,
I believe I can get a handle on it. Thank you for your time and detailed explanation/education.
Tim |
|
Back to top |
|
|
|