鱼C论坛

 找回密码
 立即注册
查看: 1375|回复: 4

[已解决]VSCod打印指针乱码

[复制链接]
发表于 2019-8-11 10:56:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 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;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-11 11:19:47 | 显示全部楼层

能说下为什么要强转么,百度上没有这一块的知识
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-11 11:59:42 | 显示全部楼层
迷雾少年 发表于 2019-8-11 11:43
std::hex
ios_base& hex (ios_base& str);
Use hexadecimal base

多谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-12-22 17:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表