About .String

Just starting out? Need help? Post your questions and find answers here.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

About .String

Post by Psychophanta »

Why this does not work?

Code: Select all

*v.string=@"hihi"
Debug *v\s
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post 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
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Re: About .String

Post 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
--Kale

Image
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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); <-/
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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.
--Kale

Image
Post Reply