const-pointer possible in PB?

Just starting out? Need help? Post your questions and find answers here.
Escobar
User
User
Posts: 17
Joined: Tue Aug 05, 2003 5:54 pm

const-pointer possible in PB?

Post by Escobar »

I have some C/C++ code that I want to translate to PB but I'm stuck with a "pointer"-problem.
How do I make an equivalent "const"-pointer in PB?

C-code: const int *pOne;

pOne is a pointer to a constant of Integer-type. The value can't be change.

Now how do I do that in PB?
Would this be valid:

*myptr.int\#

or is it pure rubbish?:wink:


Thanks in advance
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Hiya,

someone will correct me if I'm wrong here, but I think constants in Purebasic are dealt with at the pre-processor stage in that they are simply substitued for their values. Pointers to constants are therefore meaningless in this context!
Would this be valid:

*myptr.int\#

or is it pure rubbish?
The latter is true! :)
I may look like a mule, but I'm not a complete ass.
Bonne_den_kule
Addict
Addict
Posts: 841
Joined: Mon Jun 07, 2004 7:10 pm

Post by Bonne_den_kule »

Can't you use a variable instead, and get the pointer to it?
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Re: const-pointer possible in PB?

Post by traumatic »

:?:
Sorry, but what are you talking about?

const int *pOne means that while pOne can be changed within a function,
*pOne can not, as only a copy of the pointer is being passed to the function.

To gain a similar behaviour in PB, you'd have to temporarily save the value
and copy it back before exiting the procedure.

Correct me if I'm wrong.
Good programmers don't comment their code. It was hard to write, should be hard to read.
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

A const pointer is one which you cannot change the value that is being pointed to. For example:

Code: Select all

int foo;
const int *pFoo = &foo;
*pFoo = 23; /* Invalid */
In PB there is no such thing as a const pointer, you only have the normal pointer type. To do it with an int (assuming int in your C compiler is 32 bits, which it probably is under WIndows) you can do this:

Code: Select all

DefType.l foo
DefType.LONG *pOne
*pOne = @foo
*pOne\l = 23
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Post Reply