我認為“cout”函式的下一部分會產生不正確的結果,我使用“void*”對指標進行了轉換,并試圖獲取它保存的值。
<< "casted but for value: " << ((void*) (*s)) << endl;
在:
#include <iostream>
#include <cstring>
using namespace std;
int main(void)
{
char* s=(char*) malloc((12 1)*sizeof(char));
strcpy(s,"Hello World!");
cout << "direct value: " << s << endl
<< "casted for address: " << (void*) s << endl
<< "value by direct address calling: " << *s << endl
<< "casted but for value: " << ((void*) (*s)) << endl;
return 0;
}
結果:
pascal@pascal-Lenovo-ideapad-330-15AST:~/Computer/C /Programs$ g ./p10.cpp
./p10.cpp: In function ‘int main()’:
./p10.cpp:16:49: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
16 | << "casted but for value: " << ((void*) (*s)) << endl;
| ^
pascal@pascal-Lenovo-ideapad-330-15AST:~/Computer/C /Programs$ ./a.out
direct value: Hello World!
casted for address: 0x55aa8b248eb0
value by direct address calling: H
casted but for value: 0x48
uj5u.com熱心網友回復:
從評論中我注意到錯誤的想法結果是正確的,因為((void*) (*s))意味著s沒有 type的指標的值(void),這將為我們提供記錄在 char 指標中的字串的第一個字符的十六進制代碼 Ascii,所以它是明智的結果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/330041.html
上一篇:填充矩陣時訪問沖突寫入位置
