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

/ 1,309评论 / 8293阅读 / 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. A wonderful ensemble for budding scholars — or simply smarty-pants — this sensible outdated owl Halloween costume provides any child who wears it a realizing look.

  2. By his work with his father, Clarke was uncovered to many colleges of art however Art Nouveau in particular.

  3. BPD is one of the vital stigmatised and misunderstood mental health conditions and its devastating effect may be present in a thousand untold stories by those who dwell with it.

  4. Weetabix is a biscuit-formed breakfast cereal that is available in packets of two.

  5. 頬 どこまで说道:

    Use residential heat pump for pool heating?

  6. There’s one Toys’R’Us shops in Kitakyushu, in the Aeon Mall next to the closed derelict Area World amusement park.

  7. Osborne, Chris (eleven Might 2014).

  8. Angels appear in the outer lights and people on the left increase a soul up from the depths with the phrases “Out of the deep have I referred to as unto Thee” and on the correct angels guide a soul up a steep and rocky path to the shining city above with the phrases “Thou wilt present me the way in which of Life”.

  9. Free Spirit” With Cowl Art & Launch Date”.

  10. You could find a wide array of anime room decor at specialty on-line shops, together with Japanese retailers.

  11. The one required components are lasagna noodles and cheese, cheese and extra cheese.

  12. Upon becoming a member of the membership, he acquired a quantity thirty-one shirt for the side.

  13. While the heat pump is working, it’s regular for condensation water to drip from it – particularly in humid circumstances.

  14. 北条 二郎说道:

    Upon becoming a member of the membership, he acquired a quantity thirty-one shirt for the side.

  15. This is in distinction to structural parts corresponding to a roof’s decking or rafters that serve solely a roofing or structural perform and thus do not qualify for the credit.

  16. The colour scheme is actually the identical as the standard unique American colors.

  17. The rooster emerges with a sweet and sticky shell.

  18. If still you want to put it to use, that you must take some measures so as to to do.

  19. They’re set in the two-mild window in the north aisle; one light represents the Blessed Virgin Mary, and the other reveals Saint Philip Howard.

  20. Our love for this cheesy consolation food achieved a whole new stage after Kraft Foods launched the Kraft Macaroni and Cheese Dinner in 1937, a boxed macaroni and cheese that appealed to busy housewives within the U.S.

  21. LIPOZEM REVIEW说道:

    This is wonderful! The details provided is highly beneficial, and it was articulate. Thank you for making the effort to compose this.

  22. 온라인슬롯说道:

    I’m amazed, I must say. Rarely do I come across a blog that’s equally educative and entertaining, and without a doubt, you have hit the nail on the head. The problem is an issue that too few men and women are speaking intelligently about. I am very happy I found this during my hunt for something relating to this.

  23. じょしラク2说道:

    A typewriter’s ability to hit the precise spot on a type can typically surpass that of a printer.

  24. profi-teh-remont说道:

    Сервисный центр предлагает ремонт macbook 2017 ремонт macbook 2017 в петербурге

  25. profi-teh-remont说道:

    Сервисный центр предлагает замена переднего стекла xiaomi redmi note 7 pro замена дисплея xiaomi redmi note 7 pro

  26. The inhabitants of the commune are referred to as Arconceyens or Arconceyennes in French.

  27. 証券会社 sbi说道:

    A research reveals that 60 of the youngsters (thirteen to 17 years) have admitted that web addiction has become a real drawback.

  28. An urge to resolve this conflict later spurred Clarke to embrace the most reducing-edge glass know-how.

  29. David Hytner; Dominic Fifield (1 April 2008).

  30. Nevertheless, most writers declare that they might have been producing copies of glass objects from Egypt.

  31. For essentially the most entry, you could set up each a lifting lid and hinged doors.

  32. She is in a flowing hooped dress, with a darkish shawl and a parasol.

  33. Unaffiliated with any particular church, denomination or different entity, the Christian ministry creates Sunday Faculty supplies, offering complete lesson plans along with concepts for bulletin boards, crafts, object talks, games, skits, stories, songs, educating suggestions, and other assets.

  34. In any case, the goal is the artwork of crafting words, and the instruments are solely devices in the fingers of the maestro – you.

  35. 林しょうき说道:

    Personalized gadgets that reflect their favorite collection or a present card to an anime merchandise shop can be considerate.

  36. Particular Early Bird Admission: $10.00 (accessible 11 AM Friday).

  37. I don’t remember talking with the GP, but do recall being transferred to another room where three unusual individuals sat in excessive back chairs watching me as I paced the flooring.

  38. The manufacturing unit comprises eight furnaces that run continuously.

  39. During their quest he finds himself falling for her but struggles with his emotions as a consequence of his impending return to the Water Kingdom.

  40. Each time one thing comes in that has to be taken to your sales space, there’s a minimum cost of usually about $200 or extra.

  41. gifts for men说道:

    The next time I read a blog, I hope that it does not disappoint me as much as this one. I mean, I know it was my choice to read, however I actually believed you would have something helpful to talk about. All I hear is a bunch of whining about something that you could fix if you were not too busy looking for attention.

  42. Graphic card说道:

    Next time I read a blog, I hope that it won’t disappoint me just as much as this particular one. I mean, I know it was my choice to read, nonetheless I really thought you would probably have something helpful to talk about. All I hear is a bunch of crying about something you could fix if you were not too busy searching for attention.

  43. Glyco Balance说道:

    This post is fantastic. I gained plenty from going through it. The information is extremely informative and arranged.

  44. 온라인슬롯说道:

    You made some decent points there. I checked on the net for more information about the issue and found most people will go along with your views on this website.

  45. tubidy说道:

    Greetings! Very useful advice in this particular post! It is the little changes which will make the biggest changes. Many thanks for sharing!

  46. Сервисный центр предлагает ремонт телефона maxvi недорого ремонт телефонов maxvi на дому

  47. Dsouza, Stanislaus P (31 July 2010).

  48. 4476 掲示板说道:

    Business Commonplace India. Enterprise Customary.

  49. I’m very happy to uncover this site. I wanted to thank you for your time just for this wonderful read!! I definitely really liked every part of it and i also have you book marked to look at new stuff in your blog.

  50. profi teh remont说道:

    Сервисный центр предлагает сервис ремонта посудомоечных машин beko мастерские ремонта посудомоечных машин beko

发表回复

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