Differenze tra le versioni di "Ottenere il puntatore di una variabile di tipo Struttura"
Da Gambas-it.org - Wikipedia.
Riga 8: | Riga 8: | ||
* il metodo ''.Address()'' della Classe ''Object'', che ritorna l'indirizzo di memoria di un oggetto, quindi un ''Puntatore'': | * il metodo ''.Address()'' della Classe ''Object'', che ritorna l'indirizzo di memoria di un oggetto, quindi un ''Puntatore'': | ||
<FONT color=B22222>Object.Address</font>(Variabile_Struttura) As Pointer | <FONT color=B22222>Object.Address</font>(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 p 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 | ||
+ | |||
+ | |||
+ | <FONT color=gray>' ''Creiamo il "Puntatore" alla variabile di tipo "Struttura":''</font> | ||
+ | p = VarPtr(vrSt) | ||
+ | |||
+ | <FONT color=gray>' ' ' ' ' '</font> | ||
+ | |||
+ | <FONT color=gray>' ''Ora ri-otteniamo i valori presenti nella "Struttura"'' | ||
+ | ' ''dereferenziando con i "Memory-Stream" il "Puntatore" prima creato:''</font> | ||
+ | t = Memory p 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, p1 | ||
+ | Print String@(p1) | ||
+ | |||
+ | '''End''' |
Versione delle 20:28, 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 p 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": p = VarPtr(vrSt) ' ' ' ' ' ' ' Ora ri-otteniamo i valori presenti nella "Struttura" ' dereferenziando con i "Memory-Stream" il "Puntatore" prima creato: t = Memory p 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, p1 Print String@(p1) End