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

/ 11评论 / 793阅读 / 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. hitman.agency说道:

    Hey there! Do you know if they make any plugins to assist with SEO?
    I’m trying to get my site to rank for some targeted keywords but I’m not seeing
    very good results. If you know of any please share.
    Thanks! You can read similar art here: Backlink Portfolio

  2. Howdy! Do you know if they make any plugins to assist with SEO?

    I’m trying to get my website to rank for some targeted keywords but
    I’m not seeing very good gains. If you know of any please share.

    Cheers! I saw similar text here: Scrapebox List

  3. Darell说道:

    Good day! Do you know if they make any plugins to help with SEO?
    I’m trying to get my site to rank for some
    targeted keywords but I’m not seeing very good results.
    If you know of any please share. Kudos! I saw similar blog here: List of Backlinks

  4. najlepszy sklep说道:

    Hi! Do you know if they make any plugins to assist
    with Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m
    not seeing very good gains. If you know of any please share.
    Appreciate it! You can read similar blog here: Ecommerce

  5. Wow, marvelous weblog structure! How lengthy have you ever been blogging for?
    you made blogging look easy. The total look
    of your site is great, as smartly as the content! You
    can see similar here sklep internetowy

  6. Ubíquelo a través del software del sistema “Find My Mobile” que viene con el teléfono o mediante un software de localización de números de teléfonos móviles de terceros.

  7. UgBYqui说道:

    For women between the ages of 35 to 39 years, I recommended speaking with a fertility specialist after 6 months canadian pharmacy cialis

  8. TnmaFEF说道:

    com 20 E2 AD 90 20Viagra 20Natural 20Con 20Sandia 20Y 20Limon 20 20Viviviagrande viviviagrande Similarly, 107 venture backed companies were sold, down from 125 in the year earlier period what happens if a woman takes a man’s viagra

  9. escanty说道:

    Monitor Closely 2 ritonavir decreases effects of exenatide injectable suspension by Other see comment buying cialis online safe The theory is that if the body raises body temperature fever to kill bacteria, then external heat might accomplish the same purpose

  10. Clollag说道:

    cialis generic cost Molecular targeting of Akt by thymoquinone promotes G 1 arrest through translation inhibition of cyclin D1 and induces apoptosis in breast cancer cells

  11. Coikemn说道:

    A Expression of CDK2 and MAFG AS1 were positively correlated cialis online reviews

发表回复

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