Structures and Strings and Procedures... Again!

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by dmoc.

Sorry to return to this topic but I still don't understand the PB way to change a string in a structure in a procedure...

Structure MyStruct
StructStr.s
EndStructure

Procedure ChangeStr(???)
??? = "Goodbye"
EndProcedure

myvar.MyStruct
myvar\StructStr = "Hello"
TestStr(???)
Debug myvar\StructStr
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Purzel.

Hi, try this...

Code: Select all

Structure MyStruct
    StructStr.s
EndStructure

Procedure ChangeStr(*temp.MyStruct)      ;tells PB that temp is a pointer To a mystruct Structure
    *temp\StructStr= "Goodbye"           ;uses the ADRESS of temp To change Data - because the adress of temp is the adress of myvar, myvar changes now!
EndProcedure

myvar.MyStruct

myvar\StructStr = "Hello"
Debug myvar\StructStr

ChangeStr(@myvar)                        ;@ tells PB to give the adress of 'myvar' to the procedure

Debug myvar\StructStr
-Purzel-
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by dmoc.

But the point is to have a procedure that handles a string parameter regardless of whether it is a simple var or a member of a structure. Thanks for the suggestion though.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by freak.

Hmm, why don't you do it the easy way, then?

Code: Select all

Procedure.s ChangeStr(String.s)
  ProcedureReturn String + " World!"
EndProcedure

a.s = "Hello"
a = ChangeStr(a)

Debug a

; or with a Structure...

Structure Test
  Str.s
EndStructure

b.Test
b\Str = "Hello"
b\Str = ChangeStr(b\Str)

Debug b\Str

Why use Pointers, if you can live without them? :)

Timo
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Purzel.

because he aked for structures in procedures....
if you want to work with the structure and not only with one element of the structure
you have to use pointers and i thought that he wants to do that.
i missed the target i think!?

-Purzel-
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by dmoc.

Because I want to standardise and have procedures returning error codes. I am actually using proc.s in the mean time. However, the question still stands as it seems to be an anomaly with structures.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.

Code: Select all

Structure MyStruct
  StructStr.s
EndStructure

Procedure ChangeStr(*foo.MyStruct)
  *foo\StructStr = "Goodbye"
EndProcedure

myvar.MyStruct
myvar\StructStr = "Hello"
TestStr(@myvar)
Debug myvar\StructStr
--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + all updates, PB3.51, Ed3.53)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by dmoc.

Tinman, thanks but I clear see a structure in the procedure :wink:
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
Originally posted by dmoc

Tinman, thanks but I clear see a structure in the procedure :wink:
Doh, and I never noticed that I posted almost exactly the same code as someone else :)

So what is it you are looking to do? Pass a pointer to a string which is inside a structure and modify it in the procedure?

If that is what you want then you could try this:

Code: Select all

Structure sptr
  String.s
EndStructure

Structure MyStruct
  StructureUnion StrLong
    StringAddress.l
    String.s
  EndStructureUnion
EndStructure

Procedure Change(*foo.sptr)
  *foo\String = "Goodbye"
EndProcedure

MyVar.MyStruct
MyVar\StrLong\String = "Hello"
Change(@MyVar\StrLong\StringAddress)
Debug MyVar\StrLong\String
The reason you need to do it like this is because if you use @ on a string variable then you will get the address of the start of the string and not the address of where the string pointer is in the structure.

I can't rememeber if that union is correct though.


--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + all updates, PB3.51, Ed3.53)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by dmoc.

OK, this is getting closer (after removing the "StrLong"'s). If the argument actually is a structure then fine but now what if it is a simple string variable? It now won't work without first storing the simple str in a struct. Of course I could do things the other way around and store the struct str in a simple str var before/after the call (Edit; or maybe not, just checking). Either way, the same data TYPE has to be handled differently depending on if it exists as a simple var or within a structure. Maybe my reasoning is warped but shouldn't "*vs.s" be equivalent to "*MyVar\AString.s"? I know there has been a long recent post on this but I'm no clearer.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Purzel.

I guess i know now what you want!

Is it like this?

Code: Select all

Structure MyStruct
    StructStr.s
EndStructure

Procedure ChangeStr(*temp)     
    PokeS(*temp,"kukuk")
EndProcedure

myvar.MyStruct

myvar\StructStr = "Hello"
Debug myvar\StructStr

ChangeStr(@myvar\StructStr)

Debug myvar\StructStr

YourVar.s="Hello"

Debug YourVar

changestr(@YourVar)

Debug YourVar
the problem is, that the strings MUST HAVE the same length...
if the new string is smaller the problem is only some wasted memory
but if the new string is bigger you will overwrite memory and nobody knows what happens...
this is not good :)

Is this what you are looking for???

-Purzel-
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by dmoc.

Emmm, yeah, I guess, sort of :wink: I suppose I have been asking for the impossible, strings being treated as strings where-ever they come from. Anyway, I'm happy to leave it at this. Thanks for all your suggestions.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
Originally posted by dmoc

simple var or within a structure. Maybe my reasoning is warped but shouldn't "*vs.s" be equivalent to "*MyVar\AString.s"? I know
Yes, it should be and I think Fred agreed to change it to work like this the last time this subject was discussed. Using pointers is not really the problem though.

The real problem with trying to get the address of a string using the @ operator. When you use it on a normal variable you get the address of that variable. You can then access the data at that address to change the variable.

Strings are different because they are actually made up of two parts - a pointer to the string and the data in the string. When you do @string.s you are getting the address of the data in the string (as this is the most common use, to manipulate the string data). However, what you are asking for is to get the address of the pointer part of the string, so that you may change that (and therefore change the string and the data). Currently you cannot easily do that with PureBasic (Fred, maybe a @@string.s :wink:.

So you have two options - to do it like I suggested, or like Purzel suggested. One method allows you full control over the string but you have to deal with strange types. The other method allows you to change any string easily but it must fit into the already allocated string memory.

I suppose a third option would be to use a string array, since you can find the address of the start of the array (which would give you the address of the pointer to the string). But you may not like that as it is again, not dealing with simple strings.


--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + all updates, PB3.51, Ed3.53)
Post Reply