Page 1 of 1

About .String

Posted: Thu Oct 05, 2006 6:23 pm
by Psychophanta
Why this does not work?

Code: Select all

*v.string=@"hihi"
Debug *v\s

Posted: Thu Oct 05, 2006 7:34 pm
by Fluid Byte
You are using a structured pointer wich has not been initalised yet.

By using

Code: Select all

*v.string=@"hihi" 
you dont set a value for the string field but for the pointer instead.


You would have set the pointer first:

Code: Select all

*v.string = AllocateMemory(10)
*v\s = "hihi" 
Debug *v\s

Posted: Thu Oct 05, 2006 7:57 pm
by netmaestro
Pretty much on the money, but if I may nitpick just a bit:
*v.string = AllocateMemory(10)
is better written:

Code: Select all

*v.string = AllocateMemory(SizeOf(String)) 
so as to allocate the correct amount of memory and provide a better level of readability for the code.

I'm just being nitpicky, I know.

Re: About .String

Posted: Thu Oct 05, 2006 8:13 pm
by Kale
Psychophanta wrote:Why this does not work?

Code: Select all

*v.string=@"hihi"
Debug *v\s
Because you are assigning the memory address of the string pointer to your structured var, instead you should assign the actual string pointer itself. :twisted:

Code: Select all

text.s = "Testing..."
*Pointer = @text
*s.STRING = @*Pointer
Debug *s\s

Posted: Thu Oct 05, 2006 9:13 pm
by Psychophanta
i see 8)
Thanks guys!
I think the correct answer is the Kale's one

EDIT: Strings pointers and descriptor cause me headache. Another question:

Code: Select all

*Pointer = @"Testing..."
*s.STRING = @*Pointer
Debug *s\s;    ; <--- shouldn't be the same result?
Debug PeekS(*s); <-/

Posted: Thu Oct 05, 2006 10:05 pm
by Kale
Psychophanta wrote:i see 8)
Thanks guys!
I think the correct answer is the Kale's one

EDIT: Strings pointers and descriptor cause me headache. Another question:

Code: Select all

*Pointer = @"Testing..."
*s.STRING = @*Pointer
Debug *s\s;    ; <--- shouldn't be the same result?
Debug PeekS(*s); <-/
he he, no. Again you are trying to peek a string from the address of the string pointer.

In PB all strings are really pointers but PB makes these transparent to the casual user. This is why all string type sizes are 4 bytes, because these are actually stored as pointers. The pointers point to the actual strings which are inside the internal PB string buffer.

When using a structured pointer to read the memory of a string variable you are bypassing PB's friendly pointer hiding feature and you deal with the pointer in a raw way. thats why you have to de-reference this pointer (string) to find the real chars of the string.