Repository navigation
Operator Strptr String Pointer
Home › Operators › Operator Strptr (String Pointer)
Returns the address of a string's character data.
Declare Operator StrPtr ( ByRef lhs As String ) As ZString Ptr
Declare Operator StrPtr ( ByRef lhs As WString ) As WString Ptrlhs
This operator returns a ZString/WString Ptr that points to the beginning of a string/wstring's character data. Operator Strptr is the proper method for acquiring the address of a string's character data.
In case of empty String (only for variable length strings), Operator Strptr returns a null pointer.
The related Operator Varptr (Variable Pointer) and Operator @ (Address Of), when used with a String, return the address of the internal string descriptor.
When a variable length string is modified, the address of its descriptor remains always the same, but the the string's character data address (returned by Operator Strptr) may change (like any allocated memory that must be reallocated).
When a fixed length string is modified, the string's character data address (returned by Operator Strptr) is unchanged.
Note: For a variable length string, the operator returns a ZString Const Ptr (because returning by reference the string's characters pointer set in the string descriptor, this one is to be considered as read only). If the keyword Var is used to declare/initialize a user pointer from Operator Strptr, this user pointer is also defined as read only (it can not be modified further).
Warning: Dereferencing a zstring pointer to a fixed-length string of STRING*N type (which has no terminal null character) can lead to an extension of the string by some pollution, or even an outright crash.
Returns a ZString/WString Ptr to a string/wstring's character data (null value in case of empty string).
result = StrPtr ( lhs )
- New to FreeBASIC, but does exactly the same thing as
SAdd
'' This example uses Strptr to demonstrate using pointers with strings
Dim myString As String
Dim toMyStringDesc As Any Ptr
Dim toMyString As ZString Ptr
'' Note that using standard VARPTR notation will return a pointer to the
'' descriptor, not the string data itself
myString = "Improper method for Strings"
toMyStringDesc = @myString
Print myString
Print Hex( toMyStringDesc )
Print
'' However, using Strptr returns the proper pointer
myString = "Hello World Examples Are Silly"
toMyString = StrPtr(myString)
Print myString
Print *toMyString
Print
'' And the pointer acts like pointers to other types
myString = "MyString has now changed"
Print myString
Print *toMyString
PrintCategory: Operators
Kind: operator
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing