Differenze tra le versioni di "Memset ()"

Da Gambas-it.org - Wikipedia.
Riga 1: Riga 1:
 
La funzione della libreria di C
 
La funzione della libreria di C
 
  ''void * '''memset'''( void *buffer, int c, size_t count )''
 
  ''void * '''memset'''( void *buffer, int c, size_t count )''
imposta tutti i valori di un vettore (''array'') a zero.
+
copia il valore di "c" di tipo ''Byte'' nell'area di memoria (''Puntatore'' oppure ''vettore'') per una quantità stabilita nell'argomento ''count''.
  
  
 
Volendola utilizzare in Gambas, bisognerà dichiararla con ''Extern'', nonché bisognerà dichiarare la libreria di C: ''libc.so.6'', nella quale la funzione è contenuta:
 
Volendola utilizzare in Gambas, bisognerà dichiararla con ''Extern'', nonché bisognerà dichiarare la libreria di C: ''libc.so.6'', nella quale la funzione è contenuta:
  Private <FONT color=#B22222>Extern memset</font>(buffer As [Pointer], c As Byte, count As Long) In "<FONT color=#B22222>libc:6</font>"
+
  Private <FONT color=#B22222>Extern memset</font>(buffer As [vettore/puntatore], c As Byte, count As Long) In "<FONT color=#B22222>libc:6</font>"
 
Il primo valore potrà essere a seconda delle circostanze semplicemente un Puntatore, oppure un Vettore, o una Stringa.
 
Il primo valore potrà essere a seconda delle circostanze semplicemente un Puntatore, oppure un Vettore, o una Stringa.
  
  
  
Semplice esempio di uso in Gambas, nel quale si azzereranno i valori di un vbettore di tipo ''Integer[ ]'':
+
Semplice esempio di uso in Gambas, nel quale si assegna il valore 100 agli elementi di un vettore di tipo ''Byte[ ]'':
 +
<FONT color=Gray>' ''void * memset(void *buffer, int c, size_t count)''
 +
' ''Copies the character c (an unsigned char) to the first count characters of the string pointed to, by the argument buffer.''</font>
 +
Private <FONT color=#B22222>Extern memset</font>(dest As integer[], c As Byte, count As Long) In "<FONT color=#B22222>libc:6</font>"
 +
 +
 +
'''Public''' Sub Main()
 +
 +
  Dim bb As New Byte[10]
 +
  Dim b As Byte
 +
 
 +
  <FONT color=#B22222>memset</font>(bb, 100, 10)
 +
   
 +
  For Each b In bb
 +
    Print b
 +
  Next
 +
 
 +
'''End'''
 +
 
 +
 
 +
Altro esempio nel quale si azzereranno tutti gli elementi di un vettore di tipo ''Integer[ ]'':
 
  <FONT color=Gray>' ''void * memset(void *buffer, int c, size_t count)''
 
  <FONT color=Gray>' ''void * memset(void *buffer, int c, size_t count)''
 
  ' ''Copies the character c (an unsigned char) to the first count characters of the string pointed to, by the argument buffer.''</font>
 
  ' ''Copies the character c (an unsigned char) to the first count characters of the string pointed to, by the argument buffer.''</font>
Riga 17: Riga 37:
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
 
   
 
   
   Dim cc As Integer[] = [1, 2, 3, 4, 5]
+
   Dim ii As Integer[] = [1, 2, 3, 4, 5]
   Dim j As Byte
+
   Dim i As Integer
 
    
 
    
 
   
 
   
 
  <FONT color=gray>' ''Se il vettore è di tipo "Integer", allora il valore del 3° parametro della funzione''
 
  <FONT color=gray>' ''Se il vettore è di tipo "Integer", allora il valore del 3° parametro della funzione''
 
  ' ''va moltiplicato per 4, poiché il valore di tipo "Intero" occupa nella memoria 4 byte:''</font>
 
  ' ''va moltiplicato per 4, poiché il valore di tipo "Intero" occupa nella memoria 4 byte:''</font>
     <FONT color=#B22222>memset</font>(cc, 0, cc.Count * SizeOf(gb.Integer))
+
     <FONT color=#B22222>memset</font>(ii, 0, ii.Count * SizeOf(gb.Integer))
 
    
 
    
     For j = 0 To cc.Max
+
     For Each i in ii
       Print cc[j]
+
       Print i
 
     Next
 
     Next
 
   
 
   

Versione delle 15:48, 13 ott 2015

La funzione della libreria di C

void * memset( void *buffer, int c, size_t count )

copia il valore di "c" di tipo Byte nell'area di memoria (Puntatore oppure vettore) per una quantità stabilita nell'argomento count.


Volendola utilizzare in Gambas, bisognerà dichiararla con Extern, nonché bisognerà dichiarare la libreria di C: libc.so.6, nella quale la funzione è contenuta:

Private Extern memset(buffer As [vettore/puntatore], c As Byte, count As Long) In "libc:6"

Il primo valore potrà essere a seconda delle circostanze semplicemente un Puntatore, oppure un Vettore, o una Stringa.


Semplice esempio di uso in Gambas, nel quale si assegna il valore 100 agli elementi di un vettore di tipo Byte[ ]:

' void * memset(void *buffer, int c, size_t count)
' Copies the character c (an unsigned char) to the first count characters of the string pointed to, by the argument buffer.
Private Extern memset(dest As integer[], c As Byte, count As Long) In "libc:6"


Public Sub Main()

 Dim bb As New Byte[10]
 Dim b As Byte
 
  memset(bb, 100, 10)
   
  For Each b In bb
    Print b
  Next
  
End


Altro esempio nel quale si azzereranno tutti gli elementi di un vettore di tipo Integer[ ]:

' void * memset(void *buffer, int c, size_t count)
' Copies the character c (an unsigned char) to the first count characters of the string pointed to, by the argument buffer.
Private Extern memset(dest As integer[], c As Byte, count As Long) In "libc:6"

Public Sub Main()

 Dim ii As Integer[] = [1, 2, 3, 4, 5]
 Dim i As Integer
 

' Se il vettore è di tipo "Integer", allora il valore del 3° parametro della funzione
' va moltiplicato per 4, poiché il valore di tipo "Intero" occupa nella memoria 4 byte:
   memset(ii, 0, ii.Count * SizeOf(gb.Integer))
  
   For Each i in ii
     Print i
   Next

End


In quest'altro esempio si modificheranno i primi 4 caratteri di una stringa:

' void * memset(void *buffer, int c, size_t count)
' Copies the character c (an unsigned char) to the first count characters of the string pointed to, by the argument buffer.
Private Extern memset(buffer As String, c As Byte, count As Long) In "libc:6"


Public Sub Main()

 Dim s As String

  s = "Testo qualsiasi"

  memset(s, Asc("a"), 4)

  Print s

End