bin554385863 发表于 2019-8-11 10:56:51

VSCod打印指针乱码

本帖最后由 bin554385863 于 2019-8-11 10:59 编辑

#include <iostream>
#include <cstdio>
int main(int argc, char const *argv[])
{
    char str = 'p';
    /*引用 */
    char &Refstr = str;
    std::cout<<"初始值 "<<'\n'<<"str "<<"= "<<str<<std::endl;
    Refstr = 'f';
    std::cout<<"引用改值 "<<'\n'<<"str "<<"= "<<str<<std::endl;
    /*指针 */
    char *pstr = &str;
    *pstr = 'o';
    std::cout<<"指针改值 "<<'\n'<<"str "<<"= "<<str<<std::endl;
    std::cout<<"指针本身的值"<<'\n'<<"pstr "<<"= "<<std::hex<<pstr;
    //printf("%#X", pstr);
    return 0;
}

------------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 10.0.16299.1087]
(c) 2017 Microsoft Corporation。保留所有权利。

E:\Users\86184\Documents\Code>cmd /C "c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.24.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-wzyetift.545 --stdout=Microsoft-MIEngine-Out-32d0ac4s.vgx --stderr=Microsoft-MIEngine-Error-dwa3er1r.evt --pid=Microsoft-MIEngine-Pid-tkl2vw2v.0in "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi "
初始值
str = p
引用改值
str = f
指针改值
str = o
指针本身的值
pstr = o$@#
------------------------------------------------------
#include <iostream>
#include <cstdio>
int main(int argc, char const *argv[])
{
    char str = 'p';
    /*引用 */
    char &Refstr = str;
    std::cout<<"初始值 "<<'\n'<<"str "<<"= "<<str<<std::endl;
    Refstr = 'f';
    std::cout<<"引用改值 "<<'\n'<<"str "<<"= "<<str<<std::endl;
    /*指针 */
    char *pstr = &str;
    *pstr = 'o';
    std::cout<<"指针改值 "<<'\n'<<"str "<<"= "<<str<<std::endl;
    std::cout<<"指针本身的值"<<'\n'<<"pstr "<<"= ";//<<std::hex<<pstr;
    printf("%#X", pstr);
    return 0;
}

-----------------------------------------------------------------Microsoft Windows [版本 10.0.16299.1087]
(c) 2017 Microsoft Corporation。保留所有权利。

E:\Users\86184\Documents\Code>cmd /C "c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.24.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-2it5pq1h.q35 --stdout=Microsoft-MIEngine-Out-gd5u42hs.2ut --stderr=Microsoft-MIEngine-Error-kgnl53rm.5hj --pid=Microsoft-MIEngine-Pid-oaiw4ha2.nez "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi "
初始值
str = p
引用改值
str = f
指针改值
str = o
指针本身的值
pstr = 0X61FF17
E:\Users\86184\Documents\Code>
------------------------------------------
用printf就正常

迷雾少年 发表于 2019-8-11 11:06:34

#include <iostream>
#include <cstdio>
int main(int argc, char const *argv[])
{
    char str = 'p';
    /*引用 */
    char &Refstr = str;
    std::cout<<"初始值 "<<'\n'<<"str "<<"= "<<str<<std::endl;
    Refstr = 'f';
    std::cout<<"引用改值 "<<'\n'<<"str "<<"= "<<str<<std::endl;
    /*指针 */
    char *pstr = &str;
    *pstr = 'o';
    std::cout<<"指针改值 "<<'\n'<<"str "<<"= "<<str<<std::endl;
    std::cout<<"指针本身的值"<<'\n'<<"pstr "<<"= "<<std::hex<<pstr;
    //printf("%#X", pstr);
    return 0;
}


    std::cout<<"指针本身的值"<<'\n'<<"pstr "<<"= "<<std::hex<<pstr;



    std::cout<<"指针本身的值"<<'\n'<<"pstr "<<"= "<<std::hex<<(void*)pstr;

bin554385863 发表于 2019-8-11 11:19:47

迷雾少年 发表于 2019-8-11 11:06
std::cout

能说下为什么要强转么,百度上没有这一块的知识

迷雾少年 发表于 2019-8-11 11:43:17

bin554385863 发表于 2019-8-11 11:19
能说下为什么要强转么,百度上没有这一块的知识

std::hex
ios_base& hex (ios_base& str);
Use hexadecimal base
Sets the basefield format flag for the str stream to hex.

When basefield is set to hex, integer values inserted into the stream are expressed in hexadecimal base (i.e., radix 16). For input streams, extracted values are also expected to be expressed in hexadecimal base when this flag is set.

The basefield format flag can take any of the following values (each with its own manipulator):



加了std::hex的话默认后面的整型都是十六进制输出
但是这个pstr是一个char*类型的东西
cout的时候它会把pstr指向的内容当成字符串输出的
所以需要转到别的类型的整型
例如
(int*)pstr
(int)pstr
(long*)pstr
(long)pstr
(void*)pstr

bin554385863 发表于 2019-8-11 11:59:42

迷雾少年 发表于 2019-8-11 11:43
std::hex
ios_base& hex (ios_base& str);
Use hexadecimal base


{:5_109:}多谢
页: [1]
查看完整版本: VSCod打印指针乱码