簡體   English   中英

C ++ struct數據成員

[英]C++ struct data member

我在C ++,Linux工作,我遇到的問題如下:

struct testing{
uint8_t a;
uint16_t b;
char c;
int8_t d;

};

testing t;

t.a = 1;
t.b = 6;
t.c = 'c';
t.d = 4;
cout << "Value of t.a >>" << t.a << endl;
cout << "Value of t.b >>" << t.b << endl;
cout << "Value of t.c >>" << t.c << endl;
cout << "Value of t.d >>" << t.d << endl;

我的控制台上的輸出是:

Value of t.a >>
Value of t.b >>6
Value of t.c >>c
Value of t.d >>

對於int8_t和uint8_t類型,似乎缺少ta和td。 為什么會這樣?

謝謝。

int8_t和uint8_t類型可能被定義為char和unsigned char。 流<<運算符將輸出為字符。 由於它們分別設置為1和4,它們是控制字符而不是打印字符,因此控制台上不會顯示任何內容。 嘗試將它們設置為65和66('A'和'B'),看看會發生什么。

編輯:要打印出數字而不是字符,您需要將它們轉換為適當的類型:

cout << static_cast<unsigned int>(t.a) << endl;

這是因為在選擇operator<< overload時,這些變量被視為'char'類型。

嘗試:

cout << "Value of t.a >>" << static_cast<int>(t.a) << endl;

這個 linux手冊頁中,int8_t和uint8_t實際上是typedefed為char:

typedef signed char int8_t
typedef unsigned char uint8_t

並且char的值1和4是控制字符,您可以在此處找到。

這就是為什么你看不到任何印刷品。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM