Differenze tra le versioni di "Ottenere il puntatore di una variabile di tipo Struttura"
Da Gambas-it.org - Wikipedia.
Riga 25: | Riga 25: | ||
'''Public Sub Main() | '''Public Sub Main() | ||
− | Dim | + | Dim p1, p2 As Pointer |
Dim j, b As Byte | Dim j, b As Byte | ||
Dim sr As Short | Dim sr As Short | ||
Riga 44: | Riga 44: | ||
<FONT color=gray>' ''Creiamo il "Puntatore" alla variabile di tipo "Struttura":''</font> | <FONT color=gray>' ''Creiamo il "Puntatore" alla variabile di tipo "Struttura":''</font> | ||
− | + | p1 = VarPtr(vrSt) | |
<FONT color=gray>' ' ' ' ' '</font> | <FONT color=gray>' ' ' ' ' '</font> | ||
Riga 50: | Riga 50: | ||
<FONT color=gray>' ''Ora ri-otteniamo i valori presenti nella "Struttura"'' | <FONT color=gray>' ''Ora ri-otteniamo i valori presenti nella "Struttura"'' | ||
' ''dereferenziando con i "Memory-Stream" il "Puntatore" prima creato:''</font> | ' ''dereferenziando con i "Memory-Stream" il "Puntatore" prima creato:''</font> | ||
− | t = Memory | + | t = Memory p1 For Read |
Read #t, b | Read #t, b | ||
Riga 68: | Riga 68: | ||
Seek #t, 16 | Seek #t, 16 | ||
− | Read #t, | + | Read #t, p2 |
− | Print String@( | + | Print String@(p2) |
'''End''' | '''End''' |
Versione delle 20:29, 13 nov 2013
E' possibile ottenere il Puntatore di una variabile di tipo Struttura (ossia una variabile di tipo Puntatore che punta all'indirizzo di memoria di una variabile di tipo Struttura) mediante due funzioni diverse:
- la funzione VarPtr, la quale ritorna un Puntatore che punta alla variable contenuta in memoria:
VarPtr(Variabile_Struttura) As Pointer
- il metodo .Address() della Classe Object, che ritorna l'indirizzo di memoria di un oggetto, quindi un Puntatore:
Object.Address(Variabile_Struttura) As Pointer
Esempio:
Public Struct abcde a As Byte b As Short c As Integer d As Long e As String End Struct Private vrSt As Struct Abcde Public Sub Main() Dim p1, p2 As Pointer Dim j, b As Byte Dim sr As Short Dim i As Integer Dim l As Long Dim s As String Dim t As Stream With vrSt .a = 9 .b = 999 .c = 999999 .d = 999999999 .e = "testo" End With ' Creiamo il "Puntatore" alla variabile di tipo "Struttura": p1 = VarPtr(vrSt) ' ' ' ' ' ' ' Ora ri-otteniamo i valori presenti nella "Struttura" ' dereferenziando con i "Memory-Stream" il "Puntatore" prima creato: t = Memory p1 For Read Read #t, b Print b Seek #t, 2 Read #t, sr Print sr Seek #t, 4 Read #t, i Print i Seek #t, 8 Read #t, l Print l Seek #t, 16 Read #t, p2 Print String@(p2) End