Pointer often are not so easy to understand! Whats going on?
![ErNa](https://forums.parallax.com/uploads/userpics/JZIZYQ9QGBSK/n4DWIGLPA5QSR.jpg)
Never enhance a running software ![nono.gif](http://forums.parallax.com/images/smilies/nono.gif)
In DAT I create an array of 8 long elements:
Next I define another array of long values:
I output these values for test issues:
As the array is only 8 longs, value ProzHPrz is arbitrary.
But that was not the reason to make this array, just to have some values. I'd like to have addresses:
but if I dump the array:
324 + offset (16) = 340, 468 + offset = 484, as expected. But then follows just 468, and I cannot see, what I did wrong.
So I did another test:
and again: another surprise!!: 2002 came as the second element, 2004 as the fifth.
Now my brain is squirreld and I have to wait. Maybe someone else know, where my fault is?
![nono.gif](http://forums.parallax.com/images/smilies/nono.gif)
In DAT I create an array of 8 long elements:
TValue long 0, 0, 0, 0, 0, 0, 0, 0 ' 8 values for 8 traces
Next I define another array of long values:
DAT ProzHPrz long 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
I output these values for test issues:
Put9Val ( ProzHPrz, ProzHPrz (1), ProzHPrz (2), ProzHPrz(3), ProzHPrz(4), ProzHPrz(5), ProzPrz(6), ProzHPrz(7), ProzHPrz(8)) and get, as expected: 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, xxx
As the array is only 8 longs, value ProzHPrz is arbitrary.
But that was not the reason to make this array, just to have some values. I'd like to have addresses:
DAT ProzGPrz long @HPrzCmd, @TValue(0), @TValue(1), @TValue(2), @TValue(3) , @TValue(4), @TValue(5), ... and get, if I output Put9Val ( @HPrzCmd, @TValue(0), @TValue(1), @TValue(2), @TValue(3) , @TValue(4), @TValue(5), ... 340, 484, 488, 492, 496, 500, 504, 508, 512
but if I dump the array:
Put9Val ( ProzHPrz, ProzHPrz(1), ProzHPrz(2), ProzHPrz(3), ProzHPrz(4), ProzHPrz(5), ProzHPrz(6), ProzHPrz(7), ProzHPrz(8)) the result is: 324, 468, 468, 468, 468, ...
324 + offset (16) = 340, 468 + offset = 484, as expected. But then follows just 468, and I cannot see, what I did wrong.
So I did another test:
DAT ProzHPrz long @HPrzCmd, @TWert(0), 2002, @TWert(2), 2004, @TWert(4), @TWert(5), @SumPhaA and the dump showed: Put9Val ( ProzHPrz, ProzHPrz(1), ProzHPrz(2), ProzHPrz(3), ProzHPrz(4), ProzHPrz(5), ProzHPrz(6), ProzHPrz(7), ProzHPrz(8)) 324, 2002, 468, 468, 2004, 468, 468, ...
and again: another surprise!!: 2002 came as the second element, 2004 as the fifth.
Now my brain is squirreld and I have to wait. Maybe someone else know, where my fault is?
Comments
Hope that helps.
Dat ProzHPrz long @HPrzCmd, @TWert, @TWert+4, @TWert+8, @TWert+12, @TWert+16, @TWert+20, @SumPhaA
What I do is:
Create a process control block, that passes pointers to a cognew. Actually I just pass the pointer to the control block and then copy the addresse from the control block. That works fine for pointers to variables. But now my variables are elements of an array and I just want to pass a pointer to this element by getting the address of the n-th element: @Arrayname. But this array is not defined as an array, but is just a label in the data section.
So: can I define an array in the data section?
Anything in the VAR section is initialised to 0. You can actually treat any variable as an array by using the brackets after it in your code.
I can't find the reference for the rest of this but I'm pretty sure this is how it works by just testing it with the prop tool and checking the memory used.
When you declare a variable in the DAT section with square brackets like this
The value 3 will be placed in 5 times. So you would end up with the same effect as this
And if you make the 5 a 0 then the value isn't included at all.
If you want a pointer to one of the elements in array do this
Even better than the multiply is just a shift left of 1 or 2 because it will be quicker. You should also check out the bit in the prop manual about using the "@" operator in DAT sections.
So I found the solution adding index*Bytes, but still i can not see, why the 2002 is shifted by one place to the left ? Maybe, its a problem with the compiler!
Anyway, my program does again, what it did before, but uses much less code!
The @ operator in a DAT block does not do the same thing as if it was used in normal SPIN code. The @ operator in a DAT block only returns the variable's relative address. You have to use the @@ operator on this relative address to get the absolute hub address (@@MyDatVar = variable's relative address + object's base address = variable's absolute address).
This is my first post to the forum...
I tried as you show, and it worked for VAR arrays, but it didn't work for DAT arrays.
I made the following example, trying to use the
sintax (also tried
,
, etc).
I get something like:
[noparse][[/noparse]code]
Array1=
0:1->1->1
1:2->2->2
2:3->3->3
3:4->4->4
Array2=
0:1.5->3.820601e-37->1.5
1:2.5->2.204103e-39->2.5
2:3.5->2.939498e-39->3.5
3:4.5->6.019468e-34->4.5
[noparse][[/noparse]code]
How can I access the DAT values using base address?
Antonio.