Restored from previous forum. Originally posted by enisel.
Hello,
This code doesn't generate error, but the line
strb = stra
doesn't copy the whole structure content stra to strb.
Is there a way to perform this easily?
Thanks for your help.
Enisel
openconsole()
structure test
sa.l
sb.l
endstructure
stra.test
strb.test
stra\sa = 10
stra\sb = 20
strb = stra
printn(str(stra\sa))
printn(str(stra\sb))
printn(str(strb\sa))
printn(str(strb\sb))
input()
CloseConsole()
how to copy a whole structure content to another
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Berikco.
You will have to copy each element in a loop.
strb\sa = stra\sa
strb\sb = stra\sb
A CopyListItem() function is on Fred's todo list (i hope)![Smile :)](./images/smilies/icon_smile.gif)
Regards,
Berikco
http://www.benny.zeb.be/purebasic.htm
You will have to copy each element in a loop.
strb\sa = stra\sa
strb\sb = stra\sb
A CopyListItem() function is on Fred's todo list (i hope)
![Smile :)](./images/smilies/icon_smile.gif)
Regards,
Berikco
http://www.benny.zeb.be/purebasic.htm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by freak.
I always do it with CopyMemory. A Structure is nothing else than a Block of
allocated Memory, and can be used with all the Memory Commands. The addresses of the
Structured Variables can be found with the '@' in front of them.
That's it...
Timo
I always do it with CopyMemory. A Structure is nothing else than a Block of
allocated Memory, and can be used with all the Memory Commands. The addresses of the
Structured Variables can be found with the '@' in front of them.
Code: Select all
openconsole()
structure test
sa.l
sb.l
endstructure
stra.test
strb.test
stra\sa = 10
stra\sb = 20
; here we copy the contents:
CopyMemory( @stra, @strb, SizeOf( test ) )
; Some Notes:
; The first is the source, the second one the destination Address.
; the 'SizeOf( test )' command returns the Size of the Structure.
; Note that not like some other Languages, you have to use the Name of the
; Structure with SizeOf(), not the name of a Variable.
; (This is also on Fred's list, i think)
;
; The good thing about this way of copying is, if you now change the 'test' Structure, for
; example by adding a new Long Value to it, the CopyMemory() wil automatically copy this one too,
; without any further changes to be made.
printn(str(stra\sa))
printn(str(stra\sb))
printn(str(strb\sa))
printn(str(strb\sb))
Input() ; wait for Enter Key...
Timo
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by horst.
strings in a structure. It copies only the pointers,
so you will have one common string: if you change it in one
of the structures it will also be changed in the other one.
And what happens, if the original structure was in a linked
list, and you delete the element?
Horst
It should be pointed out, however, that this does not copyOriginally posted by freak
I always do it with CopyMemory.
strings in a structure. It copies only the pointers,
so you will have one common string: if you change it in one
of the structures it will also be changed in the other one.
And what happens, if the original structure was in a linked
list, and you delete the element?
Horst
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Pupil.
One small note about the copy procedure...
Just look out if there's a string in the structure, because then you just copy the pointer to the string, which means that you actually doesn't have a copy of the string content. This result, with two pointers in the structures pointing to the same memory address, could very well lead to disaster(or unwanted, unpredictible results) if one or both of these strings in the structures were to be manipulated in any way.
One small note about the copy procedure...
Just look out if there's a string in the structure, because then you just copy the pointer to the string, which means that you actually doesn't have a copy of the string content. This result, with two pointers in the structures pointing to the same memory address, could very well lead to disaster(or unwanted, unpredictible results) if one or both of these strings in the structures were to be manipulated in any way.
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Berikco.
Nothing to add Horst and Pupil![Smile :)](./images/smilies/icon_smile.gif)
I use linked list for about everything now, my current project is full of linked list
I hope Fred will add commands to copy ListElements once
(lets hope not in five years)
Regards,
Berikco
http://www.benny.zeb.be/purebasic.htm
Nothing to add Horst and Pupil
![Smile :)](./images/smilies/icon_smile.gif)
I use linked list for about everything now, my current project is full of linked list
I hope Fred will add commands to copy ListElements once
![Smile :)](./images/smilies/icon_smile.gif)
Regards,
Berikco
http://www.benny.zeb.be/purebasic.htm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm