Repository navigation
Operator Pointer Index
Home › Operators › Operator [] (Pointer Index)
Returns a reference to memory offset from an address
Declare Operator [] ( ByRef lhs As T Pointer, ByRef rhs As Integer ) ByRef As TlhsrhslhsT
This operator returns a reference to a value some distance in memory from a base address. It is essentially shorthand for "*(lhs + rhs)" because the reference can be thought of as a pointer having as value the memory location "(lhs + rhs)", and which is implicitly dereferenced; both do exactly the same thing.
Like pointer arithmetic, any type of Pointer can be indexed except for an Any Pointer. Also, like pointer arithmetic, it is up to the user to make sure meaningful data is being accessed.
When indexing a '2-dimensional' pointer (i.e. a T Ptr Ptr), the first (leftmost) index is applied before the second: For example, Pt[I1][I2] = *(Pt[I1] + I2) = *(*(Pt + I1) + I2)
In general, when using an 'n-dimensional' pointer: Pt[I1][I2].....[In], the index order (from left to right) corresponds to the dereferencing order.
This operator must not be used in case of null pointer because reference is undefined (inducing runtime error).
Otherwise, the user must ensure that the offset value (rhs) is in a range that allows an access to valid memory. Outside this range, results are undefined.
This operator can be overloaded for user-defined types as a member Operator using the appropriate syntax.
result = lhs [ rhs ]
or
lhs [ rhs ] = value- New to FreeBASIC
Operator * (Value Of)Operator [] (String Index)Operator () (Array Index)Operator + (Add)Operator - (Subtract)- Pointer Operators
'' initialize a 5-element array
Dim array(4) As Integer = { 0, 1, 2, 3, 4 }
'' point to the first element
Dim p As Integer Ptr = @array(0)
'' use pointer indexing to output array elements
For index As Integer = 0 To 4
Print p[index];
Next
PrintWill give the output,
0 1 2 3 4Category: 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