Hello
Still the pointer problem
I try this :
var a1 : int = 111 echo repr(a1) echo repr(a1.addr()) var s1 : string = "alma" echo repr(s1) echo repr(s1.addr())
i get this :
111 <---- OK ref 000000000042F8B0 --> 111 0000000002670058"alma" <---- ????? ref 000000000042F8A8 --> 0000000002670058"alma"
I understand that a1 is a simple variable. But : What is s1 ? is it a pointer to another memory area ?
(But if it is true , why not possible to write this
echo s1[] ?
s1 is a pointer or a variable ?
)
Thank you in advance
s1.addr() is ptr string while s1 is string.
ptr string means a pointer/reference to that string so that's why you see that.
string is not char *, also cstring strictly speaking is not too but it's a compatible with char *.
thanks mashingan :
But in which memory adress can i see 'a' ? ('a' is first character of "alma")
ref 000000000042F8A8 --> 0000000002670058"alma"
k1, string and seqs are some sort of magic objects in Nim. For example both are generally allocated on the heap, but have copy semantics. I have never tried to really understand all internals of seq and string, as it is sufficient to understand how they behave. Internals may change in later Nim versions.
The address of the first character in a string s is addr(s[0]) -- this address may be what we need when we send a Nim string to C library function accepting C char pointer.
echo castptr char[] may work, maybe even without the cast. But we do not need that.
Thank both of you
So this is the memory address of the first char :
echo repr(addr(s1[0]))
and s1 is internally a pointer, but we can't handle it as a pointer
thank you again
In a way, I think string is about same as fat pointer (or maybe pascal-string? )
Maybe like struct string { char *text; int length; }; , this way the len function always O(1) on the contrary to strlen which O(n)