Pointer to String Constant

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Pointer to String Constant

Post 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
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
Blood
Enthusiast
Enthusiast
Posts: 161
Joined: Tue Dec 08, 2009 8:34 pm
Location: United Kingdom

Re: Pointer to String Constant

Post by Blood »

Why would you want a pointer to a constant?
C provides the infinitely-abusable goto statement, and labels to branch to. Formally, the goto is never necessary, and in practice it is almost always easy to write code without it. We have not used goto in this book. -- K&R (2nd Ed.) : Page 65
User avatar
Tenaja
Addict
Addict
Posts: 1948
Joined: Tue Nov 09, 2010 10:15 pm

Re: Pointer to String Constant

Post 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.
User avatar
Blood
Enthusiast
Enthusiast
Posts: 161
Joined: Tue Dec 08, 2009 8:34 pm
Location: United Kingdom

Re: Pointer to String Constant

Post 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.
C provides the infinitely-abusable goto statement, and labels to branch to. Formally, the goto is never necessary, and in practice it is almost always easy to write code without it. We have not used goto in this book. -- K&R (2nd Ed.) : Page 65
User avatar
Tenaja
Addict
Addict
Posts: 1948
Joined: Tue Nov 09, 2010 10:15 pm

Re: Pointer to String Constant

Post 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?
User avatar
Blood
Enthusiast
Enthusiast
Posts: 161
Joined: Tue Dec 08, 2009 8:34 pm
Location: United Kingdom

Re: Pointer to String Constant

Post 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.
C provides the infinitely-abusable goto statement, and labels to branch to. Formally, the goto is never necessary, and in practice it is almost always easy to write code without it. We have not used goto in this book. -- K&R (2nd Ed.) : Page 65
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: Pointer to String Constant

Post 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
User avatar
Blood
Enthusiast
Enthusiast
Posts: 161
Joined: Tue Dec 08, 2009 8:34 pm
Location: United Kingdom

Re: Pointer to String Constant

Post by Blood »

Why would you need a pointer to a constant though?
C provides the infinitely-abusable goto statement, and labels to branch to. Formally, the goto is never necessary, and in practice it is almost always easy to write code without it. We have not used goto in this book. -- K&R (2nd Ed.) : Page 65
User avatar
Tenaja
Addict
Addict
Posts: 1948
Joined: Tue Nov 09, 2010 10:15 pm

Re: Pointer to String Constant

Post by Tenaja »

Blood--
Nobody has even mentioned changing the constant. Sometimes procs use pointers to strings to read them, not alter them.
User avatar
Tenaja
Addict
Addict
Posts: 1948
Joined: Tue Nov 09, 2010 10:15 pm

Re: Pointer to String Constant

Post by Tenaja »

...and sometimes the procedure is actually a Function in a DLL, which is expecting a pointer.
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: Pointer to String Constant

Post 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.
User avatar
Blood
Enthusiast
Enthusiast
Posts: 161
Joined: Tue Dec 08, 2009 8:34 pm
Location: United Kingdom

Re: Pointer to String Constant

Post 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.
C provides the infinitely-abusable goto statement, and labels to branch to. Formally, the goto is never necessary, and in practice it is almost always easy to write code without it. We have not used goto in this book. -- K&R (2nd Ed.) : Page 65
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Pointer to String Constant

Post 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

Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: Pointer to String Constant

Post 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
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: Pointer to String Constant

Post 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
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Post Reply