pointer to a string
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Tomio.
Hello,
I thought I had understand the pointer stuff, but now....
What is this?
a$="123"
*c.s=a$
*c is a pointer so if the line above is allowed at all, I would expect that the address of a$ is copied to *c.
But this seems not to be true:
a$="123"
*c.s=a$
Debug *c
a$="1111"
Debug *c
...gives "123" in both Debug lines.
So what is going on in the assignment: *c.s=a$ ?
My 'real' problem is:
I have the address of a string in a variable of type long. How can I assign it to a string-pointer?
I've searched the topics but couldn't find out.
Thanks for help
../tomio
Hello,
I thought I had understand the pointer stuff, but now....
What is this?
a$="123"
*c.s=a$
*c is a pointer so if the line above is allowed at all, I would expect that the address of a$ is copied to *c.
But this seems not to be true:
a$="123"
*c.s=a$
Debug *c
a$="1111"
Debug *c
...gives "123" in both Debug lines.
So what is going on in the assignment: *c.s=a$ ?
My 'real' problem is:
I have the address of a string in a variable of type long. How can I assign it to a string-pointer?
I've searched the topics but couldn't find out.
Thanks for help
../tomio
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by freak.
> My 'real' problem is:
> I have the address of a string in a variable of type long. How can I assign it to a string-pointer?
I don't know, about the other Stuff, but the easiest way to get the String is to
use PeekS(address), where address is just your Long Value.
another Way (without peeking the String, only pointers:
Hope this helps...
Timo
> My 'real' problem is:
> I have the address of a string in a variable of type long. How can I assign it to a string-pointer?
I don't know, about the other Stuff, but the easiest way to get the String is to
use PeekS(address), where address is just your Long Value.
another Way (without peeking the String, only pointers:
Code: Select all
Structure StringPointer
StructureUnion
Pointer.l
String.s
EndStructureUnion
EndStructure
Text.s = "Hello World"
Address.l = @Text
p.StringPointer
p\Pointer = Address
Debug p\String
Timo
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Pupil.
Try this, there might be a better way but it's a start atleast..I don't know what's going on with the examples you've provided, but it seems *c.s is treated like any normal variable and not like a pointer, this is just my layman opinion -any experts out there who want to correct me and set things straight?
Try this, there might be a better way but it's a start atleast..I don't know what's going on with the examples you've provided, but it seems *c.s is treated like any normal variable and not like a pointer, this is just my layman opinion -any experts out there who want to correct me and set things straight?
Code: Select all
Structure Test
StructureUnion
ptr.l
string.s
EndStructureUnion
EndStructure
a$ = "Test string"
c.Test\ptr = @a$
Debug a$
Debug c\ptr
Debug c\string
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Kale.
is this more what you want?
--Kale
New to PureBasic and falling in Love!
is this more what you want?
Code: Select all
a.s="123"
*c=@a
Debug *c
b.s="a big string here, blah blah blah!"
*d=@b
Debug *d
Debug PeekS(*c)
Debug PeekS(*d)
New to PureBasic and falling in Love!
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by PB.
> a$="123" : *c.s=a$ : Debug *c
> a$="1111" : Debug *c
> "123" in both Debug lines?
That's right. *c holds the value of a$ at the time you assigned a$
to it. *c is not dynamic. In the second line, you assigned "1111"
to a$ but you didn't do *c.s=a$ again, so naturally *c still holds
"123" from before.
> a$="123" : *c.s=a$ : Debug *c
> a$="1111" : Debug *c
> "123" in both Debug lines?
That's right. *c holds the value of a$ at the time you assigned a$
to it. *c is not dynamic. In the second line, you assigned "1111"
to a$ but you didn't do *c.s=a$ again, so naturally *c still holds
"123" from before.
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Tomio.
Hello,
thanks to the many replies. Let me answer one after another:
Timo, Pupil,
I know these kind of contructions. But I still hope there is a more simple way to assign a value to that string-pointer.
Kale,
the 2. part of your example is the same as the 1. part. Just new variables.
PB,
that's the point (from my point of understanding)!
*c holds the value. But *c is a pointer, so the value it holds should be the address (of a$) and nothing more. Not the content! This has nothing to do with dynamic.
../tomio
Hello,
thanks to the many replies. Let me answer one after another:
Timo, Pupil,
I know these kind of contructions. But I still hope there is a more simple way to assign a value to that string-pointer.
Kale,
the 2. part of your example is the same as the 1. part. Just new variables.
PB,
that's the point (from my point of understanding)!
*c holds the value. But *c is a pointer, so the value it holds should be the address (of a$) and nothing more. Not the content! This has nothing to do with dynamic.
../tomio
-
BackupUser
- PureBasic Guru

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

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by El_Choni.
Tomio:
You should use:
not:
If *c is a pointer, it's not a string. The @ gives the pointer to a variable.
El_Choni
Tomio:
You should use:
Code: Select all
*c = @a$
Code: Select all
*c.s = a$
El_Choni
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Tomio.
> You should use:
> *c = @a$
> not:
> *c.s = a$
If you use *c, how do you use it here: FindString(*c,"b",1)
Won't work, because "bad parameter"
But Findstring(PeekS(*c),"b",1) works.
But PeekS() copies the content.
Still my questions remains:
If *c=@a$ is allowed, why not *c.s .It's a pointer to a string-address.
../tomio
tomio
> You should use:
> *c = @a$
> not:
> *c.s = a$
If you use *c, how do you use it here: FindString(*c,"b",1)
Won't work, because "bad parameter"
But Findstring(PeekS(*c),"b",1) works.
But PeekS() copies the content.
Still my questions remains:
If *c=@a$ is allowed, why not *c.s .It's a pointer to a string-address.
../tomio
tomio
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by El_Choni.
From PureBasic's help:
El_Choni
From PureBasic's help:
So variables preceded with * (when you're not designating a member of a structure) are long values containing an address. *c.s should fire an error, IMHO.To use a pointer, put * before the variable name. A pointer is a long variable which stores an address. [...]
There are only three valid methods to set the value of a pointer:
* Get the result from a function (as shown in the [omitted] above example)
* Copy the value from another pointer
* Find the address of a variable, procedure or label (as shown below)
El_Choni
-
BackupUser
- PureBasic Guru

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

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Tomio.
...
It is generally associated with a structured type.
...
And as an example: *MyScreen.Screen
So why should *c.s be wrong?
Fred, would you be so kind and help to understand?
../tomio
tomio
But you forgot what the documentation still says:Originally posted by El_Choni
From PureBasic's help:
So variables preceded with * (when you're not designating a member of a structure) are long values containing an address. *c.s should fire an error, IMHO.To use a pointer, put * before the variable name. A pointer is a long variable which stores an address. [...]
There are only three valid methods to set the value of a pointer:
* Get the result from a function (as shown in the [omitted] above example)
* Copy the value from another pointer
* Find the address of a variable, procedure or label (as shown below)
El_Choni
...
It is generally associated with a structured type.
...
And as an example: *MyScreen.Screen
So why should *c.s be wrong?
Fred, would you be so kind and help to understand?
../tomio
tomio
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by tinman.
--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.40)
In theory *c.s is a valid variable in PureBasic. It would simply be a pointer to a string. However, because of the way PureBasic handles pointers to simple types, you cannot access the string without doing something like what Pupil suggested.Originally posted by Tomio
And as an example: *MyScreen.Screen
So why should *c.s be wrong?
--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.40)
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by El_Choni.
Tomio:
*MyScreen.Screen is still a pointer, in this case, to a structure. It's always a pointer (or it's meant to be used as a pointer, even if you can do *c=1).
tinman:
I think that accepting *c.s as a pointer to a variable could be confusing. If it is a pointer, that is, a long value, why append an .s? If you explain why would it be useful or what advantages it would bring, I will support it too. In the meantime, *c.s is behaving like if there was no * preppended.
El_Choni
Tomio:
*MyScreen.Screen is still a pointer, in this case, to a structure. It's always a pointer (or it's meant to be used as a pointer, even if you can do *c=1).
tinman:
I think that accepting *c.s as a pointer to a variable could be confusing. If it is a pointer, that is, a long value, why append an .s? If you explain why would it be useful or what advantages it would bring, I will support it too. In the meantime, *c.s is behaving like if there was no * preppended.
El_Choni
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by tinman.
The reason for adding a .s at the end is because it is a pointer to a string, the same way you do *MyScreen.Screen for a pointer to a Screen structure. If PB was able to accept "*c.s = @a$" and only copy the address of the string (at the moment it copies the string) to the *c pointer and allow you to use *c as a string then that would make more sense. I think. This is what Tomio described as what he thought would work.
But it doesn't seem to work like that.
--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.40)
OK, I did not realise that using *c.s works as a string rather than a string pointer.Originally posted by El_Choni
I think that accepting *c.s as a pointer to a variable could be confusing. If it is a pointer, that is, a long value, why append an .s? If you explain why would it be useful or what advantages it would bring, I will support it too. In the meantime, *c.s is behaving like if there was no * preppended.
The reason for adding a .s at the end is because it is a pointer to a string, the same way you do *MyScreen.Screen for a pointer to a Screen structure. If PB was able to accept "*c.s = @a$" and only copy the address of the string (at the moment it copies the string) to the *c pointer and allow you to use *c as a string then that would make more sense. I think. This is what Tomio described as what he thought would work.
But it doesn't seem to work like that.
--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.40)