Diferencia entre revisiones de «sizeof()»

De ArduWiki
Saltar a: navegación, buscar
m (Advertencias)
(Ejemplo)
Línea 20: Línea 20:
 
Para así obtener el tamaño en cantidad de elementos y no en bytes.
 
Para así obtener el tamaño en cantidad de elementos y no en bytes.
  
== Ejemplo ==
+
== Ejemplo 1 ==
 
Creamos una matriz de caracteres con cierto valor y luego mostramos carácter a carácter.
 
Creamos una matriz de caracteres con cierto valor y luego mostramos carácter a carácter.
 +
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
const char matriz[]="Esto es una matriz";
+
const byte matriz[]="Esto es una matriz";
 
void setup(){
 
void setup(){
 
   Serial.begin(115200);
 
   Serial.begin(115200);
}
 
void loop(){
 
 
   for (byte i=0; i<sizeof(matriz)-1; i++{
 
   for (byte i=0; i<sizeof(matriz)-1; i++{
       Serial.print(i);
+
       Serial.write(matriz[i]);     //Caracter
 
       Serial.print(" = ");
 
       Serial.print(" = ");
       Serial.println(matriz[i]);
+
       Serial.println(matriz[i]);   //Codigo ASCII
       delay(500);
+
       delay(100);
 
   }
 
   }
  delay(5000);
+
}
 +
void loop(){
 +
  //Nada
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Revisión del 14:05 13 jul 2018

Descripción

Evalúa el tamaño en bytes de una variable o matriz. Acepta cualquier tipo de datos: char, byte, int, long, float, etc.

Sintexis

sizeof(variable);

Parámetros

variable
nombre de variable o matriz a evaluar, que puede ser tipo: char, byte, int, long, float.

Retorna

Número de bytes ocupados por variable o matriz.

Advertencias

sizeof() al contar en bytes, no necesariamente lo hace en cantidad de elementos; esto depende del tipo de dato. Lo más correcto sería:

sizeof(matriz) / sizeof(tipo)

Para así obtener el tamaño en cantidad de elementos y no en bytes.

Ejemplo 1

Creamos una matriz de caracteres con cierto valor y luego mostramos carácter a carácter.

const byte matriz[]="Esto es una matriz";
void setup(){
   Serial.begin(115200);
   for (byte i=0; i<sizeof(matriz)-1; i++{
      Serial.write(matriz[i]);     //Caracter
      Serial.print(" = ");
      Serial.println(matriz[i]);   //Codigo ASCII
      delay(100);
   }
}
void loop(){
   //Nada
}

Vea tambien

Referencias