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

/ 1,311评论 / 8297阅读 / 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. With Molde thus qualifying for Europa League Group A, Horvath joined Brad Friedel, Kasey Keller, Tim Howard, and Brad Guzan as the one American goalkeepers ever to play in the UEFA Europa League/UEFA Cup group stage.

  2. This web page was last edited on 1 Could 2024, at 16:44 (UTC).

  3. St Michael & All Angels Michaelstow, Cornwall 1906 Davis designed a 3-gentle window for this church by means of James Powell and Sons.

  4. Good post. I learn something totally new and challenging on sites I stumbleupon every day. It will always be helpful to read articles from other authors and use something from their web sites.

  5. k-lab 株価说道:

    Even the flesh of the figures was normally white.

  6. New Braunfels Civic & Convention Center, 375 S Castell Avenue, New Braunfels.

  7. He wins his first match in the tournament prelims however loses to Luo GuanYan as a consequence of his streetfighting model being predictable.

  8. Properly-made acrylic equipment with the frosty, pastel look of sea glass are engaging; funky coloured metal gadgets with a 1950’s drink set look are enjoyable.

  9. They’d methods of depicting rays of gentle that they put into practice ten years earlier than most of their rivals attempted such dramatic atmospheric results within the 1870s.

  10. 借金王说道:

    Throughout the two-day Day of the Useless get together, which is Mexico’s largest annual celebration and a national holiday as well, the main focus is all about remembering liked ones who have passed on.

  11. The work was coated in John Harries e-book, Discovering Stained Glass, which highlighted how she painted the lettering and her detailed figures.

  12. Clark, Rosalind; City of Albany (1983).

  13. If you’re claiming the credit only for bills for certified enhancements to an existing house or for an addition or renovation to an current house, verify the “No” field.

  14. The Viper has a sporadic production schedule, but every mannequin released can hold its own with the quickest supercars on the planet.

  15. rebirth ro说道:

    After looking over a few of the blog articles on your blog, I honestly appreciate your technique of blogging. I saved as a favorite it to my bookmark site list and will be checking back soon. Please visit my web site as well and let me know how you feel.

  16. 国富町 空手说道:

    We at the moment are in a position to sell these delay pedals in a good amount so they are often obtainable on the web site.

  17. Lady Dillion never once suspected she had a past life.

  18. Some might actually have a small door that opens in the front for extra access.

  19. In an try and recapture Gi-tae, Pil-seong decides to recruit his buddies (who had been overwhelmed up by Gi-tae earlier) and find him inside a home along with his girlfriend, Kyeong-joo.

  20. I quite like looking through a post that can make men and women think. Also, thank you for allowing me to comment.

  21. Of that sum, 85,000 were in traditional album gross sales.

  22. 色川なつ说道:

    Sun: 10 AM-5 PM.

  23. Sunil Chhetri began his profession with Metropolis Club, a local membership primarily based in Delhi and joined them through the 2001-02 season.

  24. 슬롯사이트说道:

    Pretty! This has been a really wonderful article. Many thanks for supplying these details.

  25. Сервисный центр предлагает срочный ремонт пнв починить пнв

  26. profi-teh-remont说道:

    Сервисный центр предлагает замена динамика highscreen boost 3 se замена тачскрина highscreen boost 3 se

  27. How a lot did the average household make annually in 1900?

  28. Blue Dream说道:

    Aw, this was an extremely nice post. Taking the time and actual effort to produce a great article… but what can I say… I procrastinate a lot and don’t manage to get nearly anything done.

  29. She and Ming Lang need to learn martial arts but will not be allowed as a result of Ming Tian forbidding it.

  30. Lesley Demarse说道:

    Oh my goodness! Amazing article dude! Thank you, However I am encountering problems with your RSS. I don’t understand the reason why I am unable to subscribe to it. Is there anybody getting the same RSS problems? Anybody who knows the solution will you kindly respond? Thanx!

  31. profi-teh-remont说道:

    Сервисный центр предлагает ремонт lenovo thinkpad edge e440 цены ремонт lenovo thinkpad edge e440 цены

  32. Lasandra Phebus说道:

    Greetings! Very useful advice in this particular post! It’s the little changes which will make the biggest changes. Thanks a lot for sharing!

  33. Taking over an interest in stained glass while there, glasswork turned her professional aspiration.

  34. This can be a cool shop with a lot of resale Star Wars toys, but no vintage.

  35. The intrigue surrounding the recipe grew to become so intense, it gave birth to a preferred city legend, which goes like this: A woman and her daughter ask a clerk if they can have the cookie recipe.

  36. When you and your spouse are filing jointly and also you every have totally different major homes with certified fuel cell property prices, provide on line 7b the handle of your important house.

  37. This page definitely has all the information and facts I wanted about this subject and didn’t know who to ask.

  38. Usually within the case of home windows with ornate foliage, the archaeologically right surrounds are at variance with the type of the rondels which make no attempt to reproduce the medieval.

  39. Dominic Fifield (sixteen July 2003).

  40. It works by pulling the water into the impeller face after which expelling the water back out to the pool through its slotted side.

  41. A panel of employees with a background or body of elaborate arches and columns indicated that they weren’t in an atypical workshop, but laboring in a celestial workshop.

  42. At its core, a Land Rover is constructed to get people as far away from civilization as doable.

  43. And, as of late, paints of many colors are available to replicate the results of sun and age.

  44. kurumsal lisans说道:

    Kurumsal Key | Sosyal medya yönetiminde etkin ve yaratıcı çözümler sunarak markanızın bilinirliğini artırıyoruz. Kurumsal Key’e güvenin.

  45. It is best to take dark clothing on a trip, particularly if you are bringing pants or dresses.

  46. Gachapon (additionally spelled gatchapon or gashapon) capsule vending machines are Everywhere in Akihabara, however this retailer will get talked about right here because it is arguably the most important of its variety, because of the variety of machines they have, and since they have a bit in back where you possibly can straight up purchase the prizes without the randomness built into the machines.

  47. The location consists of articles on numerous matters, including leadership, preaching, discipleship, teams, communication, culture, family, resources, restoration, theology, and worship.

  48. 大企業 投資说道:

    A: Greater than 1.76 billion candy canes are made yearly for the Christmas season.

  49. Audi is a German manufacturer that produces quite a lot of sensible cars, however they do have this R8 with a V10 engine that can top 200 mph.

  50. tubidy说道:

    Way cool! Some extremely valid points! I appreciate you writing this post and also the rest of the website is really good.

发表回复

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