Pointers and references: Difference between revisions

Content added Content deleted
(Added EchoLisp)
(Added FreeBASIC)
Line 580: Line 580:


There are not other possible operations on Fortran "pointers" (at least before Fortran 2003).
There are not other possible operations on Fortran "pointers" (at least before Fortran 2003).

=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64

Type Cat
name As String
age As Integer
End Type

Type CatInfoType As Sub (As Cat Ptr)

Sub printCatInfo(c As Cat Ptr)
Print "Name "; c->name, "Age"; c-> age
Print
End Sub

' create Cat object on heap and store a pointer to it
Dim c As Cat Ptr = New Cat

' set fields using the pointer and the "crow's foot" operator
c->name = "Fluffy"
c->age = 9

' print them out through a procedure pointer
Dim cit As CatInfoType = ProcPtr(printCatInfo)
cit(c)

Delete c
c = 0

Dim i As Integer = 3
' create an integer pointer variable and set it to the address of 'i'
Dim pi As Integer Ptr = @i

'change the variable through the pointer
*pi = 4

'print out the result
print "i ="; *pi

'create a reference to the variable i
Dim ByRef As Integer j = i

' set j (and hence i) to a new value
j = 5

' print them out
Print "i ="; i, "j ="; j
Sleep</lang>

{{out}}
<pre>
Name Fluffy Age 9

i = 4
i = 5 j = 5
</pre>

=={{header|Go}}==
=={{header|Go}}==
Go has pointers but no pointer arithmetic. In general it disallows many operations that are common in other languages but have proven to be common sources of programming errors.
Go has pointers but no pointer arithmetic. In general it disallows many operations that are common in other languages but have proven to be common sources of programming errors.