Page 1 of 3

Posted: Sun Dec 29, 2002 10:32 pm
by BackupUser
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

Posted: Sun Dec 29, 2002 10:57 pm
by BackupUser
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:

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

Hope this helps...

Timo

Posted: Sun Dec 29, 2002 10:59 pm
by BackupUser
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?

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

Posted: Mon Dec 30, 2002 1:40 am
by BackupUser
Restored from previous forum. Originally posted by Kale.

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

New to PureBasic and falling in Love! :)

Posted: Mon Dec 30, 2002 1:47 am
by BackupUser
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.

Posted: Mon Dec 30, 2002 11:20 am
by BackupUser
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

Posted: Mon Dec 30, 2002 12:13 pm
by BackupUser
Restored from previous forum. Originally posted by freak.

Isn't 'PeekS(address)' simple enough?

Posted: Mon Dec 30, 2002 2:19 pm
by BackupUser
Restored from previous forum. Originally posted by El_Choni.

Tomio:

You should use:

Code: Select all

*c = @a$
not:

Code: Select all

*c.s = a$
If *c is a pointer, it's not a string. The @ gives the pointer to a variable.

El_Choni

Posted: Mon Dec 30, 2002 2:39 pm
by BackupUser
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

Posted: Mon Dec 30, 2002 3:52 pm
by BackupUser
Restored from previous forum. Originally posted by El_Choni.

From PureBasic's help:
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)
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.


El_Choni

Posted: Mon Dec 30, 2002 5:03 pm
by BackupUser
Restored from previous forum. Originally posted by Kale.

pionters are longs containing memory addresses, not strings, which is correct!

*c.s = a$ New to PureBasic and falling in Love! :)

Posted: Mon Dec 30, 2002 5:06 pm
by BackupUser
Restored from previous forum. Originally posted by Tomio.
Originally posted by El_Choni

From PureBasic's help:
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)
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.


El_Choni
But you forgot what the documentation still says:
...
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

Posted: Mon Dec 30, 2002 5:48 pm
by BackupUser
Restored from previous forum. Originally posted by tinman.
Originally posted by Tomio

And as an example: *MyScreen.Screen
So why should *c.s be wrong?
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.

--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.40)

Posted: Mon Dec 30, 2002 8:50 pm
by BackupUser
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

Posted: Tue Dec 31, 2002 12:34 am
by BackupUser
Restored from previous forum. Originally posted by tinman.
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.
OK, I did not realise that using *c.s works as a string rather than a string pointer.

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)