34 lines
957 B
C++
34 lines
957 B
C++
#include "smt4497-P2.h"
|
|
|
|
int p2_struct_union()
|
|
{
|
|
setlocale(LC_ALL, "spanish");
|
|
|
|
printf("\nEstructuras | PRÁCTICA 2 | SS\n");
|
|
printf("---------------------------------\n");
|
|
|
|
struct {
|
|
int entero;
|
|
char car;
|
|
} vEstructura = { 256, 1 }; // Define + inicializa la estructura
|
|
|
|
union {
|
|
int entero;
|
|
unsigned char car[4];
|
|
} vUnion = { 256 }; // Define + inicializa la unión
|
|
|
|
vUnion.entero = 0x89abcdef; // byte 0: 0xef, byte 1: 0xcd, byte 2: 0xab, byte 3: 0x89
|
|
|
|
printf("vUnion.entero vale 0x%x\n", vUnion.entero);
|
|
printf("El valor del byte 2 de vUnion.entero es: 0x%x\n",
|
|
// (vUnion.entero & 0x00ff0000) >> 16); también es válido hacer primero la máscara y luego shift
|
|
(vUnion.entero >> (8 * 2) & 0xff));
|
|
|
|
printf("El valor del byte 2 de vUnion.entero es: 0x%x\n",
|
|
vUnion.car[2]);
|
|
|
|
// printf("Estructura: %d Tam: %d\n", vEstructura.entero, sizeof(vEstructura));
|
|
// printf("Union: %d Tam: %d\n", vUnion.entero, sizeof(vUnion));
|
|
|
|
return 0;
|
|
} |