Page 1 of 2

Pointer to String Constant

Posted: Sun Nov 06, 2011 12:18 am
by nco2k
i know there are workarounds for this, but it would be nice to have it natively:

Code: Select all

#String = "Test"
Debug @#String
c ya,
nco2k

Re: Pointer to String Constant

Posted: Sun Nov 06, 2011 2:06 pm
by Blood
Why would you want a pointer to a constant?

Re: Pointer to String Constant

Posted: Sun Nov 06, 2011 4:38 pm
by Tenaja
Well for one, to pass the pointer to a procedure.

PB is very weak with constants and array initialization. Although I much prefer it to C, I have been contemplating doing some libraries in C just to avoid the kludgey workarounds.

Re: Pointer to String Constant

Posted: Sun Nov 06, 2011 6:05 pm
by Blood
Tenaja wrote:Well for one, to pass the pointer to a procedure.
That doesn't answer my question. Why do you need a pointer to a constant.

Re: Pointer to String Constant

Posted: Sun Nov 06, 2011 7:04 pm
by Tenaja
You don't NEED one--there is always a work-around. But, then again, with IF, you don't NEED While or For, either. I'm sure the majority of PB's features could be removed if we only included those that were NEEDED.

If you have a proc that handles strings, why limit it to variable strings????

Are Constants SUPPOSED to be second-rate data structures that have limited options?

Re: Pointer to String Constant

Posted: Sun Nov 06, 2011 7:27 pm
by Blood
I'm sorry, i do not agree at all with this request. Constants are constants and should never be changed, therefore you don't need a pointer to them, you can pass them by value to a function. If you needed a pointer to a constant because you want to change it, then that is a variable so use one. Would you ever need a pointer to a constant that never changes? If so whats wrong with using it by value?

While and for loops serve a good purpose so of course they are required because they cut down on repetitious code.

Just adding features without thinking it through is not a good design philosophy.

Re: Pointer to String Constant

Posted: Sun Nov 06, 2011 7:52 pm
by PMV
A Constant is nothing more then a datasection inside of your code.
You can need a pointer to data and only read that ... no need to change.
So constants will be still constants. And it is already possible to use
the pointer from Datasections and literal-constants. It is only not allowed
to use the pointer-symbol at a constant itself. I don't know if i would
use that, if it is possible ... but it is not bad to be possible to get the
pointer.

MFG PMV

Re: Pointer to String Constant

Posted: Sun Nov 06, 2011 7:54 pm
by Blood
Why would you need a pointer to a constant though?

Re: Pointer to String Constant

Posted: Sun Nov 06, 2011 7:55 pm
by Tenaja
Blood--
Nobody has even mentioned changing the constant. Sometimes procs use pointers to strings to read them, not alter them.

Re: Pointer to String Constant

Posted: Sun Nov 06, 2011 8:00 pm
by Tenaja
...and sometimes the procedure is actually a Function in a DLL, which is expecting a pointer.

Re: Pointer to String Constant

Posted: Sun Nov 06, 2011 8:11 pm
by PMV
Blood wrote:Why would you need a pointer to a constant though?
Why would you need pointers?
The same answer goes for the other question.

Re: Pointer to String Constant

Posted: Sun Nov 06, 2011 8:36 pm
by Blood
All i want is that a explanation is given where you actually need a pointer to a constant. I'm willing to learn i just don't get why you would need a pointer to something which will never change rather than just deal with the value.

Re: Pointer to String Constant

Posted: Sun Nov 06, 2011 9:19 pm
by Shield
It's probably not the best example (since you could define the function differently and use a string parameter directly)
but it shows why something like this can be useful.

Code: Select all


#NewTitle = "New Title"

CompilerIf #PB_Compiler_Unicode
	Import ""
		SomeExternalFunction(hWnd.i, *lpString.i) As "_SetWindowTextW"
	EndImport
CompilerElse
	Import ""
		SomeExternalFunction(hWnd.i, *lpString.i) As "_SetWindowTextA"
	EndImport	
CompilerEndIf

OpenWindow(0, 0, 0, 400, 260, "Old Title", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
SomeExternalFunction(WindowID(0), @"New Title")
SomeExternalFunction(WindowID(0), #NewTitle) ; Yep, that's one example on why you need it.

Repeat
	If WaitWindowEvent() = #PB_Event_CloseWindow
		Break
	EndIf
ForEver
End


Re: Pointer to String Constant

Posted: Sun Nov 06, 2011 10:01 pm
by helpy

Code: Select all

#String1 = "Test X"
#String2 = "Test X"
#String3 = "Test 3"
#String4 = "Test 4"
#String5 = "Test 5"
#String6 = "Test 6"
#String7 = "Test X"
#String8 = "Test 8"
#String9 = "Test X"

CompilerIf #PB_Compiler_Unicode
	Prototype ptAddrOfStringConstant( StringConstant.p-unicode )
CompilerElse
	Prototype ptAddrOfStringConstant( StringConstant.p-ascii )
CompilerEndIf

Procedure _AddrOffStringConstant( *StringConstant )
	ProcedureReturn *StringConstant
EndProcedure

AddrOfStringConstant.ptAddrOfStringConstant = @_AddrOffStringConstant()

Debug AddrOfStringConstant( #String1 )
Debug AddrOfStringConstant( #String2 )
Debug AddrOfStringConstant( #String3 )
Debug AddrOfStringConstant( #String4 )
Debug AddrOfStringConstant( #String5 )
Debug AddrOfStringConstant( #String6 )
Debug AddrOfStringConstant( #String7 )
Debug AddrOfStringConstant( #String8 )
Debug AddrOfStringConstant( #String9 )
#String1, #String2, #String7, and #String9 are pointing to the same string constant, because the string is identical!

cu, guido

Re: Pointer to String Constant

Posted: Sun Nov 06, 2011 11:47 pm
by nco2k
@Blood
> Why would you want a pointer to a constant?
mostly because of winapi and 3rd party libs:

Code: Select all

wndclass.WNDCLASSEX\cbSize = SizeOf(WNDCLASSEX)
wndclass\lpszClassName = @"MyClassName"
;wndclass\lpszClassName = @#String

Code: Select all

CallFunctionFast(*Function, @String$)
;CallFunctionFast(*Function, @#String)
sure you can workaround it, but @"MyClassName" is a pointer to a constant too and it works, so why shouldnt @#String?

> If you needed a pointer to a constant because you want to change it
who said anything about changing?

c ya,
nco2k