How do I make a pointer to the first element in an array in Red/System?
如何在Red / System中创建指向数组中第一个元素的指针?
Assigning an address to a pointer is no problem:
为指针分配地址没有问题:
my-integer: 1
ptr: declare pointer! [integer!]
ptr: :my-integer
The array is declared.
数组已声明。
buffer: as int-ptr! allocate 1009 * size? integer!
but.
ptr: :buffer
is not the way, nor is.
不是道路,也不是。
ptr: ::buffer
ptr: :buffer/1
ptr: :(buffer/1)
Anyone knows how to do this?
谁知道怎么做?
Regards,
Arnold
1 个解决方案
#1
5
As both ptr and buffer are pointers to integer data, you simply assign one to the other:
由于ptr和buffer都是指向整数数据的指针,因此您只需将一个指定给另一个:
ptr: buffer
The :variable
syntax is only required to get the address of what would be called "primitive" types in Java. That equates to byte!, integer!, float!, float32! and logic! in the current version of Red/System. Without the leading :
, the compiler will provide the value stored in the variable.
:变量语法只需要获取Java中所谓的“原始”类型的地址。这相当于byte!,integer!,float!,float32!和逻辑!在当前版本的Red / System中。如果没有前导:,编译器将提供存储在变量中的值。
All other types such as c-string! and struct! (and hence alias!) are in fact pointers. So the compiler provides their value when they are referenced, which is a memory address.
所有其他类型,如c-string!和结构! (因此别名!)实际上是指针。因此编译器在引用时提供它们的值,这是一个内存地址。
When you reference a word, the Red/System compiler provides the value stored in it:
引用单词时,Red / System编译器提供存储在其中的值:
print i ;; will print the value stored in i
When you use a set-word (a variable with a :
appended to the name), the compiler stores a value in it:
当您使用set-word(带有:附加到名称的变量)时,编译器会在其中存储一个值:
i: 1 ;; stores 1 in variable i
When you use a get-word (a variable with a :
inserted at the beginning of the name), the compiler provides the address of the variable.
当您使用get-word(在名称的开头插入一个带有:的变量)时,编译器会提供变量的地址。
int-ptr: :i ;; stores the address of i in int-ptr
#1
5
As both ptr and buffer are pointers to integer data, you simply assign one to the other:
由于ptr和buffer都是指向整数数据的指针,因此您只需将一个指定给另一个:
ptr: buffer
The :variable
syntax is only required to get the address of what would be called "primitive" types in Java. That equates to byte!, integer!, float!, float32! and logic! in the current version of Red/System. Without the leading :
, the compiler will provide the value stored in the variable.
:变量语法只需要获取Java中所谓的“原始”类型的地址。这相当于byte!,integer!,float!,float32!和逻辑!在当前版本的Red / System中。如果没有前导:,编译器将提供存储在变量中的值。
All other types such as c-string! and struct! (and hence alias!) are in fact pointers. So the compiler provides their value when they are referenced, which is a memory address.
所有其他类型,如c-string!和结构! (因此别名!)实际上是指针。因此编译器在引用时提供它们的值,这是一个内存地址。
When you reference a word, the Red/System compiler provides the value stored in it:
引用单词时,Red / System编译器提供存储在其中的值:
print i ;; will print the value stored in i
When you use a set-word (a variable with a :
appended to the name), the compiler stores a value in it:
当您使用set-word(带有:附加到名称的变量)时,编译器会在其中存储一个值:
i: 1 ;; stores 1 in variable i
When you use a get-word (a variable with a :
inserted at the beginning of the name), the compiler provides the address of the variable.
当您使用get-word(在名称的开头插入一个带有:的变量)时,编译器会提供变量的地址。
int-ptr: :i ;; stores the address of i in int-ptr