[c++]使用int()显式类型转换指针来修改const char的值

/ 120评论 / 1766阅读 / 0点赞

摘要

记得之前看过一篇文章说所有的指针其实都可以视为一个int类型,这句话催生了我很多想法,这篇博文即为其一。

示例

#include<iostream>
using namespace std;

int main()
{
    const char str[] = "123456";
    cout << str << endl;
    char* p = (char*)(int(str));
    *p = 48;    //0的ASCII
    cout << str << endl;

    return 0;
}

不可修改的特例

#include<iostream>
using namespace std;

int main()
{
    const char* str = "123456";
    cout << str << endl;
    char* p = (char*)(int(str));
    *p = 48;    //0的ASCII
    cout << str << endl;

    return 0;
}

写在最后

* c++本身也提供了一些方法可以去掉str的const限制:比如const_cast<>,它的使用方法在百度上很多,如果需要请自行百度了解。

* 指针着实是相当有意思的东西。

  1. You are so interesting! I do not think I’ve truly read something like that before. So great to find somebody with some original thoughts on this subject matter. Really.. thank you for starting this up. This website is one thing that is required on the web, someone with a little originality.

  2. 789 bet说道:

    Way cool! Some very valid points! I appreciate you penning this post and the rest of the website is very good.

  3. Great site you’ve got here.. It’s hard to find quality writing like yours nowadays. I truly appreciate individuals like you! Take care!!

  4. I would like to thank you for the efforts you’ve put in penning this blog. I really hope to see the same high-grade blog posts by you later on as well. In truth, your creative writing abilities has encouraged me to get my own, personal website now 😉

  5. 78win说道:

    Pretty! This has been an incredibly wonderful post. Thanks for providing these details.

  6. 78win说道:

    I couldn’t resist commenting. Very well written!

  7. Tory Laditka说道:

    Hi, I do think this is a great website. I stumbledupon it 😉 I’m going to come back yet again since I book-marked it. Money and freedom is the best way to change, may you be rich and continue to help other people.

  8. Duncan Disano说道:

    Saved as a favorite, I love your web site!

  9. Zackary Platner说道:

    Nice post. I learn something new and challenging on blogs I stumbleupon on a daily basis. It will always be helpful to read through content from other writers and practice something from their websites.

  10. lehenga choli说道:

    I love reading through an article that will make men and women think. Also, thank you for allowing for me to comment.

  11. I used to be suggested this web site through my cousin. I am now not positive whether or not this put up is written via him as no one else recognize such detailed about my problem. You’re incredible! Thank you!

  12. lehenga choli说道:

    Having read this I thought it was very informative. I appreciate you taking the time and energy to put this article together. I once again find myself personally spending a significant amount of time both reading and posting comments. But so what, it was still worth it!

  13. I absolutely love your blog.. Very nice colors & theme. Did you build this amazing site yourself? Please reply back as I’m planning to create my own personal blog and would love to know where you got this from or what the theme is called. Kudos.

  14. Having read this I believed it was really enlightening. I appreciate you finding the time and effort to put this content together. I once again find myself personally spending a lot of time both reading and commenting. But so what, it was still worth it.

  15. iptv说道:

    Everything is very open with a precise clarification of the issues. It was definitely informative. Your website is very helpful. Many thanks for sharing.

  16. iptv说道:

    Excellent blog you’ve got here.. It’s difficult to find quality writing like yours nowadays. I really appreciate people like you! Take care!!

  17. iptv说道:

    Pretty! This has been an incredibly wonderful article. Many thanks for supplying this info.

  18. iptv说道:

    I’d like to thank you for the efforts you’ve put in writing this site. I’m hoping to check out the same high-grade content by you in the future as well. In fact, your creative writing abilities has encouraged me to get my own blog now 😉

  19. whoah this blog is magnificent i love reading your articles. Keep up the great work! You know, a lot of people are hunting around for this information, you can help them greatly.

  20. iptv说道:

    Hi! I simply want to give you a big thumbs up for the great info you have right here on this post. I’ll be returning to your site for more soon.

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注