如何像在 C 中一样打印出 Nim 地址:
int array[] = { 7, 8, 9 };
printf(" %p ", (void *)&array);
Run Code Online (Sandbox Code Playgroud)
?尝试:
var
arr = newSeq[array[2,int]](2)
refVar = addr arr
echo refVar
Run Code Online (Sandbox Code Playgroud)
给了:
Error: type mismatch: got <ptr seq[array[0..1, int]]>
but expected one of:
proc echo(x: varargs[typed, `$`])
first type mismatch at position: 1
required type for x: varargs[typed]
but expression 'refVar' is of type: ptr seq[array[0..1, int]
Run Code Online (Sandbox Code Playgroud)
请大家帮忙!
小智 5
用于repr获取还包含内存地址的值的字符串表示形式:
var
arr = newSeq[array[2,int]](2)
refVar = addr arr
echo arr.repr # 0x7f5c2fcbd050@[[0, 0], [0, 0]]
echo refVar.repr # ptr 0x564bd37d7528 --> 0x7f5c2fcbd050@[[0, 0], [0, 0]]
Run Code Online (Sandbox Code Playgroud)