mercredi 7 octobre 2020

Why is the output of cout and printf() inconsistent in the following code, cout not output correctly? [duplicate]

#include <iostream>
using namespace std;
#pragma pack(1) //
typedef struct {
uint8_t ID;
uint8_t CONFIG1;
} REG_MAP;
#pragma pack(0)
int main() {
    REG_MAP* reg;
    uint8_t arr[2]  {1,2};
    reg = (REG_MAP*) arr;
    uint8_t temp = reg->ID;
    cout << dec << reg->ID << endl;
    cout << temp << endl;
    if(temp == 1){
        cout << "temp is 1" << endl;
    }
    printf("%d",reg->ID);
}

I tried to assign an array to a structure of the same size, but found that coutcould not print reg ID correctly, but printf()could, which made me puzzled.

enter image description here

Aucun commentaire:

Enregistrer un commentaire